commands.go (view raw)
1package main
2
3import (
4 "strings"
5
6 "github.com/bwmarrin/discordgo"
7)
8
9var (
10 handlersMap map[string]BotCommand
11 shortCommands = map[string]string{}
12)
13
14func InitHandlers() {
15 handlersMap = map[string]BotCommand{
16 "echo": {ShortCode: "e", Handler: handleEcho, Help: "echoes a message"},
17 "prefix": {ShortCode: "pre", Handler: handlePrefix, Help: "sets the bot's prefix"},
18 "help": {ShortCode: "h", Handler: handleHelp, Help: "shows this help message"},
19 }
20
21 for command, botCommand := range handlersMap {
22 if botCommand.ShortCode == "" {
23 continue
24 }
25 shortCommands[botCommand.ShortCode] = command
26 }
27}
28
29func handleHelp(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
30 helpText := "**Bot commands:**\n"
31 for command, botCommand := range handlersMap {
32 helpText += "* " + botCommand.FormatHelp(command) + "\n"
33 }
34 return helpText
35}
36
37func handleEcho(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
38 return strings.Join(args, " ")
39}
40
41func handlePrefix(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
42 if len(args) == 0 {
43 return "Usage: prefix <new prefix>"
44 }
45
46 newPrefix := args[0]
47 if len(newPrefix) > 10 {
48 return "Prefix is too long"
49 }
50
51 config.Values.Prefix = newPrefix
52 err := config.Save()
53 if err != nil {
54 logger.Errorf("could not save config: %s", err)
55 }
56
57 return "Prefix set to " + newPrefix
58}