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() { func init() {
commands["ban"] = command{ commands = append(commands, &command{
staffOnly: true, staffOnly: true,
name: "ban",
validate: func(cmd commandInit) bool { validate: func(cmd commandInit) bool {
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles) 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 return fmt.Sprintf("%s was banned by %s", target, cmd.message.Author.Username), nil
}, },
help: "Ban a player", help: "Ban a player",
} })
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,10 +18,12 @@ import (
// Register commands to this map // Register commands to this map
var ( var (
commands = make(map[string]command) commands = make([]*command, 0)
commandMap = make(map[string]*command)
messageRoleMap = make(map[string]map[string]string) messageRoleMap = make(map[string]map[string]string)
memeRateLimit *rateLimit memeRateLimit *rateLimit
embedColor = 0x007D96
) )
type commandInit struct { type commandInit struct {
@ -35,9 +37,13 @@ type commandInit struct {
type command struct { type command struct {
staffOnly bool staffOnly bool
deleteInvocation bool
echo bool
// TODO Does this really need to exist separately? // TODO Does this really need to exist separately?
validate func(cmd commandInit) bool validate func(cmd commandInit) bool
run func(cmd commandInit) (string, error) run func(cmd commandInit) (string, error)
name string
aliases []string
help 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 messageRoleMap[messageRole.MessageID][messageRole.Emoji] = messageRole.RoleID
} }
// Init commandMap
Echo(cfg) 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(readyHandler())
bot.AddHandler(leaveHandler(cfg)) bot.AddHandler(leaveHandler(cfg))
bot.AddHandler(commandHandler(cfg, db, sapi, twitterClient)) 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 { func sendEmbed(s *discordgo.Session, channelID string, embed *discordgo.MessageEmbed) *discordgo.Message {
embed.Color = embedColor
msg, err := s.ChannelMessageSendEmbed(channelID, embed) msg, err := s.ChannelMessageSendEmbed(channelID, embed)
if err != nil { if err != nil {
beaver.Errorf("could not send embed: %v", err) 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]) cmdArg := strings.ToLower(args[0])
cmd, ok := commands[cmdArg] cmd, ok := commandMap[cmdArg]
if !ok { if !ok {
return 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) sendMessage(s, m.ChannelID, "You cannot run this command.", false)
return 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) feedback, err := cmd.run(cmdInit)
if err != nil { if err != nil {
feedback = "Internal error" feedback = "Internal error"

View File

@ -5,44 +5,57 @@ import (
"strings" "strings"
"go.etztech.xyz/sedbot/config" "go.etztech.xyz/sedbot/config"
"github.com/bwmarrin/discordgo"
) )
func Echo(cfg *config.Config) { func Echo(cfg *config.Config) {
echoes := make([]string, 0)
for _, e := range cfg.Echoes { for _, e := range cfg.Echoes {
echo := e commands = append(commands, &command{
commands[echo.Name] = command{ name: e.Name,
aliases: e.Aliases,
echo: true,
validate: func(cmd commandInit) bool { validate: func(cmd commandInit) bool {
return true return true
}, },
run: func(cmd commandInit) (string, error) { run: func(cmd commandInit) (string, error) {
return echo.Message, nil return e.Message, nil
}, },
help: echo.Help, help: e.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))
} }
commands["echoes"] = command{ commands = append(commands, &command{
deleteInvocation: true,
name: "echoes",
validate: func(cmd commandInit) bool { validate: func(cmd commandInit) bool {
return true return true
}, },
run: func(cmd commandInit) (string, error) { 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", help: "Get all dynamic messages",
} })
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,7 +3,9 @@ package discord
import "go.etztech.xyz/sedbot/imgur" import "go.etztech.xyz/sedbot/imgur"
func init() { func init() {
cmd := command{ commands = append(commands, &command{
name: "jupiter",
aliases: []string{"jup", "jupjup"},
validate: func(cmd commandInit) bool { validate: func(cmd commandInit) bool {
return true return true
}, },
@ -15,9 +17,5 @@ func init() {
return img.Link, nil return img.Link, nil
}, },
help: "Get a Jupiter image", 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" const bannedPlayersFile = "banned-players.json"
func init() { func init() {
commands["register"] = command{ commands = append(commands, &command{
name: "register",
validate: func(cmd commandInit) bool { validate: func(cmd commandInit) bool {
return len(cmd.message.Member.Roles) == 0 return len(cmd.message.Member.Roles) == 0
}, },
@ -94,7 +95,7 @@ func init() {
return "", nil return "", nil
}, },
help: "Register yourself with the Discord", help: "Register yourself with the Discord",
} })
} }
type Ban struct { type Ban struct {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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