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 "shoot": {ShortCode: "sh", Handler: handleShoot, Help: "shoots a random user in your voice channel"},
18 "prefix": {Handler: handlePrefix, Help: "sets the bot's prefix"},
19 "help": {ShortCode: "h", Handler: handleHelp, Help: "shows this help message"},
20 }
21
22 for command, botCommand := range handlersMap {
23 if botCommand.ShortCode == "" {
24 continue
25 }
26 shortCommands[botCommand.ShortCode] = command
27 }
28}
29
30func handleEcho(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
31 return strings.Join(args, " ")
32}
33
34func handlePrefix(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
35 if len(args) == 0 {
36 return "Usage: " + formatCommand("prefix <new prefix>")
37 }
38
39 newPrefix := args[0]
40 if len(newPrefix) > 10 {
41 return "Prefix is too long."
42 }
43
44 config.Values.Prefix = newPrefix
45 err := config.Save()
46 if err != nil {
47 logger.Errorf("could not save config: %s", err)
48 }
49
50 return "Prefix set to " + formatCommand("")
51}
52
53func handleHelp(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
54 helpText := "**Bot commands:**\n"
55 for command, botCommand := range handlersMap {
56 helpText += "* " + botCommand.FormatHelp(command) + "\n"
57 }
58 return helpText
59}