src/commands.go (view raw)
1package src
2
3import (
4 "fmt"
5 "strings"
6
7 gl "github.com/BiRabittoh/disgord/src/globals"
8 "github.com/BiRabittoh/disgord/src/music"
9 "github.com/BiRabittoh/disgord/src/shoot"
10 "github.com/bwmarrin/discordgo"
11)
12
13const (
14 MsgUnknownCommand = "Unknown command: %s."
15 MsgPrefixSet = "Prefix set to `%s`."
16 MsgPrefixTooLong = "Prefix is too long."
17 MsgUsagePrefix = "Usage: %s <new prefix>."
18 MsgHelp = "**Bot commands:**\n%s"
19 MsgHelpCommandFmt = "* %s\n"
20)
21
22var (
23 handlersMap map[string]gl.BotCommand
24 shortCommands = map[string]string{}
25)
26
27func InitHandlers() {
28 handlersMap = map[string]gl.BotCommand{
29 "echo": {ShortCode: "e", Handler: handleEcho, Help: "echoes a message"},
30 "shoot": {ShortCode: "sh", Handler: shoot.HandleShoot, Help: "shoots a random user in your voice channel"},
31 "prefix": {Handler: handlePrefix, Help: "sets the bot's prefix for this server"},
32 "play": {ShortCode: "p", Handler: music.HandlePlay, Help: "plays a song from youtube"},
33 "pause": {ShortCode: "pa", Handler: music.HandlePause, Help: "pauses the current song"},
34 "resume": {ShortCode: "r", Handler: music.HandleResume, Help: "resumes the current song"},
35 "skip": {ShortCode: "s", Handler: music.HandleSkip, Help: "skips the current song"},
36 "queue": {ShortCode: "q", Handler: music.HandleQueue, Help: "shows the current queue"},
37 "clear": {ShortCode: "c", Handler: music.HandleClear, Help: "clears the current queue"},
38 "leave": {ShortCode: "l", Handler: music.HandleLeave, Help: "leaves the voice channel"},
39 "help": {ShortCode: "h", Handler: handleHelp, Help: "shows this help message"},
40 }
41
42 for command, botCommand := range handlersMap {
43 if botCommand.ShortCode == "" {
44 continue
45 }
46 shortCommands[botCommand.ShortCode] = command
47 }
48}
49
50func HandleCommand(s *discordgo.Session, m *discordgo.MessageCreate) (response string, ok bool, err error) {
51 command, args, ok := gl.ParseUserMessage(m.Content, m.GuildID)
52 if !ok {
53 return
54 }
55
56 longCommand, short := shortCommands[command]
57 if short {
58 command = longCommand
59 }
60
61 botCommand, found := handlersMap[command]
62 if !found {
63 response = fmt.Sprintf(MsgUnknownCommand, gl.FormatCommand(command, m.GuildID))
64 return
65 }
66
67 response = botCommand.Handler(args, s, m)
68 return
69}
70
71func handleEcho(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
72 return strings.Join(args, " ")
73}
74
75func handlePrefix(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
76 if len(args) == 0 {
77 return fmt.Sprintf(MsgUsagePrefix, gl.FormatCommand("prefix", m.GuildID))
78 }
79
80 newPrefix := args[0]
81 if len(newPrefix) > 10 {
82 return MsgPrefixTooLong
83 }
84
85 gl.SetPrefix(m.GuildID, newPrefix)
86
87 return fmt.Sprintf(MsgPrefixSet, newPrefix)
88}
89
90func handleHelp(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
91 helpText := MsgHelp
92 for command, botCommand := range handlersMap {
93 // helpText += fmt.Sprintf()
94 helpText += fmt.Sprintf(MsgHelpCommandFmt, botCommand.FormatHelp(command, m.GuildID))
95 }
96 return helpText
97}