/
discord.go
92 lines (77 loc) · 2.37 KB
/
discord.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package civsession
import (
"strings"
"github.com/bwmarrin/discordgo"
"github.com/ecshreve/civ-bot-go/internal/util"
)
// CommandsHandler handles all civ-bot commands.
func (cs *CivSession) CommandsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself.
if m.Author.ID == s.State.User.ID {
return
}
// Ignore all messages that don't have the /civ prefix.
if !strings.HasPrefix(m.Content, "/civ") {
return
}
// Get the arguments.
args := strings.Split(m.Content, " ")[1:]
// Ensure command exists.
if len(args) == 0 {
s.ChannelMessageSend(m.ChannelID, util.ErrorMessage("command missing", "for a list of commands type `/civ help`"))
return
}
// Call the corresponding handler.
switch args[0] {
case "help":
helpCommandHandler(s, m)
case "config":
cs.configHandler(s, m)
case "new", "oops":
cs.newCommandHandler(s, m)
case "info":
cs.infoCommandHandler(s, m)
case "list":
cs.listCommandHandler(s, m)
case "ban":
cs.banCommandHandler(s, m, args)
default:
s.ChannelMessageSend(m.ChannelID, util.ErrorMessage("invalid command", "for a list of available commands, type `/civ help`"))
}
}
// ReactionsHandler handles all civ-bot related reactions.
func (cs *CivSession) ReactionsHandler(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
// Ignore all reactions created by the bot itself.
if r.UserID == s.State.User.ID {
return
}
// Fetch some extra information about the message associated to the reaction.
m, err := s.ChannelMessage(r.ChannelID, r.MessageID)
// Ignore reactions on messages that have an error or that have not been sent by the bot.
if err != nil || m == nil || m.Author.ID != s.State.User.ID {
return
}
// Ignore messages that are not embeds with a command in the footer.
if len(m.Embeds) != 1 || m.Embeds[0].Footer == nil || m.Embeds[0].Footer.Text == "" {
return
}
// Ignore reactions that haven't been set by the bot.
if !util.IsBotReaction(s, m.Reactions, &r.Emoji) {
return
}
// Ignore when sender is invalid or is a bot.
user, err := s.User(r.UserID)
if err != nil || user == nil || user.Bot {
return
}
args := m.Embeds[0].Footer.Text
// Call the corresponding handler.
switch args {
case "config":
cs.configReactionHandler(s, r, m, user)
case "new":
cs.newReactionHandler(s, r, m, user)
case "pick":
cs.pickReactionHandler(s, r, m, user)
}
}