all repos — disgord @ 41b487beb8025d82e480ed8899f028ac2d381a06

A simple Discord bot in Go.

handle prefixes server-wise
Marco Andronaco andronacomarco@gmail.com
Wed, 02 Oct 2024 10:00:15 +0200
commit

41b487beb8025d82e480ed8899f028ac2d381a06

parent

cf72b4a9d665c121e9dbb5fad81e35dc068423d2

4 files changed, 56 insertions(+), 25 deletions(-)

jump to
M config.example.jsonconfig.example.json

@@ -1,7 +1,6 @@

{ "applicationId": "your-application-id-here", "token": "your-token-here", - "prefix": "$", "magazineSize": 3, "outros": [
M src/commands.gosrc/commands.go

@@ -12,19 +12,19 @@ handlersMap map[string]BotCommand

shortCommands = map[string]string{} ) -func (bc BotCommand) FormatHelp(command string) string { +func (bc BotCommand) FormatHelp(command, guildID string) string { var shortCodeStr string if bc.ShortCode != "" { - shortCodeStr = fmt.Sprintf(" (%s)", formatCommand(bc.ShortCode)) + shortCodeStr = fmt.Sprintf(" (%s)", formatCommand(bc.ShortCode, guildID)) } - return fmt.Sprintf(helpFmt, formatCommand(command)+shortCodeStr, bc.Help) + return fmt.Sprintf(helpFmt, formatCommand(command, guildID)+shortCodeStr, bc.Help) } func InitHandlers() { handlersMap = map[string]BotCommand{ "echo": {ShortCode: "e", Handler: handleEcho, Help: "echoes a message"}, "shoot": {ShortCode: "sh", Handler: handleShoot, Help: "shoots a random user in your voice channel"}, - "prefix": {Handler: handlePrefix, Help: "sets the bot's prefix"}, + "prefix": {Handler: handlePrefix, Help: "sets the bot's prefix for this server"}, "help": {ShortCode: "h", Handler: handleHelp, Help: "shows this help message"}, }

@@ -37,7 +37,7 @@ }

} func HandleCommand(s *discordgo.Session, m *discordgo.MessageCreate) (response string, ok bool, err error) { - command, args, ok := parseUserMessage(m.Content) + command, args, ok := parseUserMessage(m.Content, m.GuildID) if !ok { return }

@@ -49,7 +49,7 @@ }

botCommand, found := handlersMap[command] if !found { - response = "Unknown command: " + formatCommand(command) + response = "Unknown command: " + formatCommand(command, m.GuildID) return }

@@ -63,7 +63,7 @@ }

func handlePrefix(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string { if len(args) == 0 { - return "Usage: " + formatCommand("prefix <new prefix>") + return "Usage: " + formatCommand("prefix <new prefix>", m.GuildID) + "." } newPrefix := args[0]

@@ -71,19 +71,15 @@ if len(newPrefix) > 10 {

return "Prefix is too long." } - Config.Values.Prefix = newPrefix - err := Config.Save() - if err != nil { - logger.Errorf("could not save config: %s", err) - } + setPrefix(m.GuildID, newPrefix) - return "Prefix set to " + formatCommand("") + return "Prefix set to " + newPrefix + "." } func handleHelp(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string { helpText := "**Bot commands:**\n" for command, botCommand := range handlersMap { - helpText += "* " + botCommand.FormatHelp(command) + "\n" + helpText += "* " + botCommand.FormatHelp(command, m.GuildID) + "\n" } return helpText }
M src/globals.gosrc/globals.go

@@ -10,8 +10,9 @@ "github.com/kkdai/youtube/v2"

) const ( - msgError = "Something went wrong." - helpFmt = "%s - _%s_" + msgError = "Something went wrong." + helpFmt = "%s - _%s_" + defaultPrefix = "$" ) var (

@@ -31,10 +32,9 @@ type MyConfig struct {

ApplicationID string `json:"applicationId"` Token string `json:"token"` - Prefix string `json:"prefix"` - - Outros []KeyValuePair `json:"outros"` - Radios []KeyValuePair `json:"radios"` + Prefixes []KeyValuePair `json:"prefixes"` + Outros []KeyValuePair `json:"outros"` + Radios []KeyValuePair `json:"radios"` MagazineSize uint `json:"magazineSize"` }
M src/utils.gosrc/utils.go

@@ -40,12 +40,12 @@ }

return } -func formatCommand(command string) string { - return fmt.Sprintf("`%s%s`", Config.Values.Prefix, command) +func formatCommand(command, guildID string) string { + return fmt.Sprintf("`%s%s`", getPrefix(guildID), command) } -func parseUserMessage(messageContent string) (command string, args []string, ok bool) { - after, found := strings.CutPrefix(messageContent, Config.Values.Prefix) +func parseUserMessage(messageContent, guildID string) (command string, args []string, ok bool) { + after, found := strings.CutPrefix(messageContent, getPrefix(guildID)) if !found { return }

@@ -54,3 +54,39 @@ userInput := strings.Split(after, " ")

command = strings.ToLower(userInput[0]) return command, userInput[1:], len(command) > 0 } + +func getPrefix(guildID string) string { + for _, prefix := range Config.Values.Prefixes { + if prefix.Name == guildID { + return prefix.Value + } + } + + Config.Values.Prefixes = append(Config.Values.Prefixes, KeyValuePair{Name: guildID, Value: defaultPrefix}) + err := Config.Save() + if err != nil { + logger.Errorf("could not save config: %s", err) + } + return defaultPrefix +} + +func setPrefix(guildID, prefixValue string) string { + var found bool + for i, prefix := range Config.Values.Prefixes { + if prefix.Name == guildID { + Config.Values.Prefixes[i].Value = prefixValue + found = true + break + } + } + + if !found { + Config.Values.Prefixes = append(Config.Values.Prefixes, KeyValuePair{Name: guildID, Value: prefixValue}) + } + + err := Config.Save() + if err != nil { + logger.Errorf("could not save config: %s", err) + } + return defaultPrefix +}