all repos — disgord @ 788b481ce887db1dccfc707bc18e508405fdd809

A simple Discord bot in Go.

src/commands.go (view raw)

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