Signed-off-by: Etzelia <etzelia@hotmail.com>
broom
Etzelia 2021-02-22 23:53:55 -06:00
parent beb2b4a6aa
commit dec436805c
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
21 changed files with 140 additions and 86 deletions

View File

@ -10,8 +10,9 @@ import (
)
func init() {
commands["ban"] = command{
commands = append(commands, &command{
staffOnly: true,
name: "ban",
validate: func(cmd commandInit) bool {
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
},
@ -49,5 +50,5 @@ func init() {
return fmt.Sprintf("%s was banned by %s", target, cmd.message.Author.Username), nil
},
help: "Ban a player",
}
})
}

View File

@ -10,7 +10,8 @@ import (
)
func init() {
commands["birb"] = command{
commands = append(commands, &command{
name: "birb",
validate: func(cmd commandInit) bool {
return true
},
@ -42,5 +43,5 @@ func init() {
return fmt.Sprintf("%d: %s\n%s", comic.Num, comic.Title, comic.Img), nil
},
help: "Get a FalseKnees comic",
}
})
}

View File

@ -9,8 +9,9 @@ import (
)
func init() {
commands["broadcast"] = command{
commands = append(commands, &command{
staffOnly: true,
name: "broadcast",
validate: func(cmd commandInit) bool {
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
},
@ -37,5 +38,5 @@ func init() {
return "Broadcast sent!", nil
},
help: "Send an in-game broadcast",
}
})
}

View File

@ -8,7 +8,8 @@ import (
const clearMax = 20
func init() {
commands["clear"] = command{
commands = append(commands, &command{
name: "clear",
validate: func(cmd commandInit) bool {
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
},
@ -64,5 +65,5 @@ func init() {
return "", cmd.session.ChannelMessagesBulkDelete(cmd.message.ChannelID, batch)
},
help: "Clear messages",
}
})
}

View File

@ -6,7 +6,8 @@ import (
)
func init() {
commands["compliment"] = command{
commands = append(commands, &command{
name: "compliment",
validate: func(cmd commandInit) bool {
return true
},
@ -38,5 +39,5 @@ func init() {
return "", nil
},
help: "Compliment someone!",
}
})
}

View File

@ -18,7 +18,8 @@ type dadJoke struct {
}
func init() {
commands["dad"] = command{
commands = append(commands, &command{
name: "dad",
validate: func(cmd commandInit) bool {
return true
},
@ -60,5 +61,5 @@ func init() {
return dj.Joke, nil
},
help: "Get a random Dad joke",
}
})
}

View File

@ -18,10 +18,12 @@ import (
// Register commands to this map
var (
commands = make(map[string]command)
commands = make([]*command, 0)
commandMap = make(map[string]*command)
messageRoleMap = make(map[string]map[string]string)
memeRateLimit *rateLimit
embedColor = 0x007D96
)
type commandInit struct {
@ -34,10 +36,14 @@ type commandInit struct {
}
type command struct {
staffOnly bool
staffOnly bool
deleteInvocation bool
echo bool
// TODO Does this really need to exist separately?
validate func(cmd commandInit) bool
run func(cmd commandInit) (string, error)
name string
aliases []string
help string
}
@ -77,7 +83,19 @@ func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error)
messageRoleMap[messageRole.MessageID][messageRole.Emoji] = messageRole.RoleID
}
// Init commandMap
Echo(cfg)
for _, c := range commands {
if c.name == "" {
beaver.Errorf("command is missing a name: %s", c.help)
continue
}
commandMap[c.name] = c
for _, a := range c.aliases {
commandMap[a] = c
}
}
bot.AddHandler(readyHandler())
bot.AddHandler(leaveHandler(cfg))
bot.AddHandler(commandHandler(cfg, db, sapi, twitterClient))
@ -123,6 +141,7 @@ func sendMessage(s *discordgo.Session, channelID, content string, scrub bool) *d
}
func sendEmbed(s *discordgo.Session, channelID string, embed *discordgo.MessageEmbed) *discordgo.Message {
embed.Color = embedColor
msg, err := s.ChannelMessageSendEmbed(channelID, embed)
if err != nil {
beaver.Errorf("could not send embed: %v", err)
@ -168,7 +187,7 @@ func commandHandler(cfg *config.Config, db *database.Database, sapi *serverapi.C
cmdArg := strings.ToLower(args[0])
cmd, ok := commands[cmdArg]
cmd, ok := commandMap[cmdArg]
if !ok {
return
}
@ -189,6 +208,11 @@ func commandHandler(cfg *config.Config, db *database.Database, sapi *serverapi.C
sendMessage(s, m.ChannelID, "You cannot run this command.", false)
return
}
if cmd.deleteInvocation {
if err := s.ChannelMessageDelete(m.Message.ChannelID, m.Message.ID); err != nil {
beaver.Warnf("could not remove invocation for %s: %v", m.Content, err)
}
}
feedback, err := cmd.run(cmdInit)
if err != nil {
feedback = "Internal error"

View File

@ -5,44 +5,57 @@ import (
"strings"
"go.etztech.xyz/sedbot/config"
"github.com/bwmarrin/discordgo"
)
func Echo(cfg *config.Config) {
echoes := make([]string, 0)
for _, e := range cfg.Echoes {
echo := e
commands[echo.Name] = command{
commands = append(commands, &command{
name: e.Name,
aliases: e.Aliases,
echo: true,
validate: func(cmd commandInit) bool {
return true
},
run: func(cmd commandInit) (string, error) {
return echo.Message, nil
return e.Message, nil
},
help: echo.Help,
}
for _, a := range echo.Aliases {
alias := a
commands[alias] = command{
validate: func(cmd commandInit) bool {
return true
},
run: func(cmd commandInit) (string, error) {
return echo.Message, nil
},
help: echo.Help,
}
}
combined := append([]string{echo.Name}, echo.Aliases...)
echoes = append(echoes, fmt.Sprintf("**%s**: %s", strings.Join(combined, ", "), echo.Help))
help: e.Help,
})
}
commands["echoes"] = command{
commands = append(commands, &command{
deleteInvocation: true,
name: "echoes",
validate: func(cmd commandInit) bool {
return true
},
run: func(cmd commandInit) (string, error) {
return strings.Join(echoes, "\n"), nil
embed := &discordgo.MessageEmbed{
Title: "Echo Commands",
Fields: make([]*discordgo.MessageEmbedField, len(cfg.Echoes)),
}
for i, echo := range cfg.Echoes {
name := echo.Name
if len(echo.Aliases) > 0 {
name += fmt.Sprintf(" (%s)", strings.Join(echo.Aliases, ", "))
}
embed.Fields[i] = &discordgo.MessageEmbedField{
Name: name,
Value: echo.Help,
Inline: true,
}
}
channel, err := cmd.session.UserChannelCreate(cmd.message.Author.ID)
if err != nil {
return "", err
}
sendEmbed(cmd.session, channel.ID, embed)
return "", nil
},
help: "Get all dynamic messages",
}
})
}

View File

@ -5,7 +5,8 @@ import (
)
func init() {
commands["fired"] = command{
commands = append(commands, &command{
name: "fired",
validate: func(cmd commandInit) bool {
return true
},
@ -14,5 +15,5 @@ func init() {
cmd.database.CheckPing(cmd.config.FiredRole)), nil
},
help: "Check how many times Carolyn has been fired.",
}
})
}

View File

@ -1,13 +1,16 @@
package discord
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
)
func init() {
commands["help"] = command{
commands = append(commands, &command{
deleteInvocation: true,
name: "help",
validate: func(cmd commandInit) bool {
return true
},
@ -30,14 +33,15 @@ func init() {
return "", nil
},
help: "HELP! HEEEEEEEEEELP!",
}
})
}
func singleHelp(cmd commandInit, arg string) *discordgo.MessageEmbed {
embed := &discordgo.MessageEmbed{
Title: "Unkown Command",
Color: 0x007D96,
}
c, ok := commands[arg]
c, ok := commandMap[arg]
if !ok {
return embed
}
@ -46,7 +50,7 @@ func singleHelp(cmd commandInit, arg string) *discordgo.MessageEmbed {
return embed
}
embed.Title = arg
embed.Title = c.name
embed.Description = c.help
return embed
}
@ -60,10 +64,17 @@ func allHelp(cmd commandInit) *discordgo.MessageEmbed {
if staff {
embed.Description = "Commands with an asterisk (*) are staff-only"
}
for n, c := range commands {
cmdName := n
for _, c := range commands {
if c.echo {
continue
}
cmdName := c.name
if len(c.aliases) > 0 {
cmdName += fmt.Sprintf(" (%s)", strings.Join(c.aliases, ", "))
}
if c.staffOnly {
cmdName += "*"
cmdName = fmt.Sprintf("*%s", cmdName)
}
if !c.staffOnly || staff {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{

View File

@ -12,7 +12,9 @@ import (
)
func init() {
cmd := command{
commands = append(commands, &command{
name: "history",
aliases: []string{"names"},
validate: func(cmd commandInit) bool {
return true
},
@ -79,8 +81,5 @@ func init() {
return "", nil
},
help: "Minecraft name history",
}
commands["history"] = cmd
commands["names"] = cmd
})
}

View File

@ -3,7 +3,8 @@ package discord
import "go.etztech.xyz/inspiro"
func init() {
commands["inspire"] = command{
commands = append(commands, &command{
name: "inspire",
validate: func(cmd commandInit) bool {
return true
},
@ -22,5 +23,5 @@ func init() {
return img, nil
},
help: "Get inspired!",
}
})
}

View File

@ -6,7 +6,8 @@ import (
)
func init() {
commands["insult"] = command{
commands = append(commands, &command{
name: "insult",
validate: func(cmd commandInit) bool {
return true
},
@ -39,5 +40,5 @@ func init() {
return "", nil
},
help: "Insult someone!",
}
})
}

View File

@ -3,7 +3,9 @@ package discord
import "go.etztech.xyz/sedbot/imgur"
func init() {
cmd := command{
commands = append(commands, &command{
name: "jupiter",
aliases: []string{"jup", "jupjup"},
validate: func(cmd commandInit) bool {
return true
},
@ -15,9 +17,5 @@ func init() {
return img.Link, nil
},
help: "Get a Jupiter image",
}
commands["jupiter"] = cmd
commands["jup"] = cmd
commands["jupjup"] = cmd
})
}

View File

@ -16,7 +16,8 @@ import (
const bannedPlayersFile = "banned-players.json"
func init() {
commands["register"] = command{
commands = append(commands, &command{
name: "register",
validate: func(cmd commandInit) bool {
return len(cmd.message.Member.Roles) == 0
},
@ -94,7 +95,7 @@ func init() {
return "", nil
},
help: "Register yourself with the Discord",
}
})
}
type Ban struct {

View File

@ -9,7 +9,9 @@ import (
)
func init() {
cmd := command{
commands = append(commands, &command{
name: "status",
aliases: []string{"version", "online"},
validate: func(cmd commandInit) bool {
return true
},
@ -22,7 +24,6 @@ func init() {
}
embed := &discordgo.MessageEmbed{
Color: 0x007D96,
Title: fmt.Sprintf("Server Status for `%s`", cmd.config.Server.Address),
Description: q.MOTD,
Fields: []*discordgo.MessageEmbedField{
@ -46,9 +47,5 @@ func init() {
return "", nil
},
help: "Get the server status",
}
commands["status"] = cmd
commands["version"] = cmd
commands["online"] = cmd
})
}

View File

@ -6,8 +6,9 @@ import (
)
func init() {
commands["tweet"] = command{
commands = append(commands, &command{
staffOnly: true,
name: "tweet",
validate: func(cmd commandInit) bool {
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
},
@ -30,5 +31,5 @@ func init() {
return fmt.Sprintf("https://twitter.com/%d/status/%d", tweet.User.ID, tweet.ID), nil
},
help: "Send a tweet from the BirbMC Twitter",
}
})
}

View File

@ -10,8 +10,9 @@ import (
)
func init() {
commands["unban"] = command{
commands = append(commands, &command{
staffOnly: true,
name: "unban",
validate: func(cmd commandInit) bool {
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
},
@ -36,5 +37,5 @@ func init() {
cmd.database.AddUnban(record)
},
help: "Unban a player",
}
})
}

View File

@ -5,8 +5,9 @@ import (
)
func init() {
commands["unbans"] = command{
commands = append(commands, &command{
staffOnly: true,
name: "unbans",
validate: func(cmd commandInit) bool {
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
},
@ -30,6 +31,6 @@ func init() {
sendEmbed(cmd.session, cmd.message.ChannelID, embed)
return "", nil
},
help: "Unban a player",
}
help: "Check the unban scheduler",
})
}

View File

@ -7,15 +7,13 @@ import (
)
func init() {
commands["welcome"] = command{
commands = append(commands, &command{
deleteInvocation: true,
name: "welcome",
validate: func(cmd commandInit) bool {
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
},
run: func(cmd commandInit) (string, error) {
if err := cmd.session.ChannelMessageDelete(cmd.message.ChannelID, cmd.message.ID); err != nil {
return "", err
}
orphans := make([]*discordgo.Member, 0)
members, err := cmd.session.GuildMembers(cmd.message.GuildID, "", 1000)
@ -49,5 +47,5 @@ func init() {
return "", nil
},
help: "Get a list of people with no roles",
}
})
}

View File

@ -10,7 +10,8 @@ import (
)
func init() {
commands["xkcd"] = command{
commands = append(commands, &command{
name: "xkcd",
validate: func(cmd commandInit) bool {
return true
},
@ -40,5 +41,5 @@ func init() {
return fmt.Sprintf("%d: %s\n%s\n%s", comic.Num, comic.SafeTitle, comic.Alt, comic.Img), nil
},
help: "Get an xkcd comic",
}
})
}