diff --git a/discord/ban.go b/discord/ban.go index de427ac..1180784 100644 --- a/discord/ban.go +++ b/discord/ban.go @@ -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", - } + }) } diff --git a/discord/birb.go b/discord/birb.go index 4f11bb3..91642c3 100644 --- a/discord/birb.go +++ b/discord/birb.go @@ -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", - } + }) } diff --git a/discord/broadcast.go b/discord/broadcast.go index 6b69c17..18de5d1 100644 --- a/discord/broadcast.go +++ b/discord/broadcast.go @@ -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", - } + }) } diff --git a/discord/clear.go b/discord/clear.go index 5a3f0e9..f75c676 100644 --- a/discord/clear.go +++ b/discord/clear.go @@ -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", - } + }) } diff --git a/discord/compliment.go b/discord/compliment.go index 3df9afa..f26066d 100644 --- a/discord/compliment.go +++ b/discord/compliment.go @@ -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!", - } + }) } diff --git a/discord/dad.go b/discord/dad.go index 7744e99..a9fb940 100644 --- a/discord/dad.go +++ b/discord/dad.go @@ -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", - } + }) } diff --git a/discord/discord.go b/discord/discord.go index 33c96b8..5b0c533 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -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" diff --git a/discord/echo.go b/discord/echo.go index b450a02..9ba244b 100644 --- a/discord/echo.go +++ b/discord/echo.go @@ -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", - } + }) } diff --git a/discord/fired.go b/discord/fired.go index 7247018..6c5c5b8 100644 --- a/discord/fired.go +++ b/discord/fired.go @@ -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.", - } + }) } diff --git a/discord/help.go b/discord/help.go index 3d80eb8..2587ef6 100644 --- a/discord/help.go +++ b/discord/help.go @@ -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{ diff --git a/discord/history.go b/discord/history.go index 3b5c26f..baa609a 100644 --- a/discord/history.go +++ b/discord/history.go @@ -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 + }) } diff --git a/discord/inspire.go b/discord/inspire.go index 2e89db9..e4238ae 100644 --- a/discord/inspire.go +++ b/discord/inspire.go @@ -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!", - } + }) } diff --git a/discord/insult.go b/discord/insult.go index ad85472..09ddce1 100644 --- a/discord/insult.go +++ b/discord/insult.go @@ -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!", - } + }) } diff --git a/discord/jupiter.go b/discord/jupiter.go index 46e7ebb..1fecc3e 100644 --- a/discord/jupiter.go +++ b/discord/jupiter.go @@ -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 + }) } diff --git a/discord/register.go b/discord/register.go index a2cc9b6..9901d3a 100644 --- a/discord/register.go +++ b/discord/register.go @@ -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 { diff --git a/discord/status.go b/discord/status.go index 4b786c6..e2afe3a 100644 --- a/discord/status.go +++ b/discord/status.go @@ -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 + }) } diff --git a/discord/tweet.go b/discord/tweet.go index 32b9269..756fedd 100644 --- a/discord/tweet.go +++ b/discord/tweet.go @@ -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", - } + }) } diff --git a/discord/unban.go b/discord/unban.go index 324b90c..b699acd 100644 --- a/discord/unban.go +++ b/discord/unban.go @@ -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", - } + }) } diff --git a/discord/unbans.go b/discord/unbans.go index 698ab60..d2d8742 100644 --- a/discord/unbans.go +++ b/discord/unbans.go @@ -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", + }) } diff --git a/discord/welcome.go b/discord/welcome.go index e4380b2..785aa5c 100644 --- a/discord/welcome.go +++ b/discord/welcome.go @@ -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", - } + }) } diff --git a/discord/xkcd.go b/discord/xkcd.go index 9491239..b498a76 100644 --- a/discord/xkcd.go +++ b/discord/xkcd.go @@ -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", - } + }) }