This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
317 lines (259 loc) · 7.62 KB
/
handlers.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"database/sql"
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
)
func addHandlers(s *discordgo.Session) {
s.AddHandler(messageHandler)
s.AddHandler(reactHandler)
s.AddHandler(leaveHandler)
s.AddHandler(joinHandler)
}
func messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// Flow Control
done := false
// Get channel structure
channel, err := s.State.Channel(m.ChannelID)
if err != nil {
fmt.Println("Couldn't get a channel structure.")
fmt.Println("Author : " + m.Author.Username)
fmt.Println("Message : " + m.Content)
fmt.Println(err.Error())
return
}
// Forward to Master.
// done = forward(s, channel, m.Message)
// if done {
// return
// }
// Get guild structure
guild, err := s.State.Guild(channel.GuildID)
if err != nil {
fmt.Println("Couldn't get a guild structure.")
fmt.Println("Channel : " + channel.Name)
fmt.Println("Author : " + m.Author.Username)
fmt.Println("Message : " + m.Content)
fmt.Println(err.Error())
return
}
// Get the fame channel
fame, err := getFameChannel(s, guild)
if err == sql.ErrNoRows {
} else if err != nil {
// Maybe the channel was deleted. Do not return.
printDiscordError("Couldn't get the hall of fame for this guild.", guild, channel, m.Message, nil, err)
} else if channel.ID == fame.ID && m.Message.Type == discordgo.MessageTypeChannelPinnedMessage {
// "Miiko pinned a message to this channel. See all the pins."
err = s.ChannelMessageDelete(channel.ID, m.Message.ID)
if err != nil {
printDiscordError("Couldn't delete a message.", guild, channel, m.Message, nil, err)
}
return
}
// Myself? Super User?
if m.Author.ID == me.ID || m.Author.Discriminator == "0000" {
return
}
// Get guild member
member, err := s.GuildMember(channel.GuildID, m.Author.ID)
if err != nil {
printDiscordError("Couldn't get a member structure.", guild, channel, m.Message, nil, err)
return
}
// First time setup for welcome channels
if m.Type == discordgo.MessageTypeGuildMemberJoin {
// Check if there's already one
_, err := selectWelcomeChannel(guild)
if err == sql.ErrNoRows {
// Set the welcome channel
_, err := setWelcomeChannel(guild, channel)
if err != nil {
printDiscordError("Couldn't select the welcome channel.", guild, channel, m.Message, nil, err)
}
// Ask for guard, because the joinHandler probably couldn't handle it
askForGuard(s, guild, m.Message.Author)
} else if err != nil {
printDiscordError("Couldn't select the welcome channel.", guild, channel, m.Message, nil, err)
}
return
}
// Mentionned someone?
if len(m.Mentions) == 1 {
// Mentionned me?
if m.Mentions[0].ID == me.ID {
// Split
command := strings.Split(m.Content, " ")
// Redirect commands
if len(command) > 1 {
switch command[1] {
case "prune":
// Permissions
has, err := canAdministrate(s, guild, member)
if err != nil {
printDiscordError("Couldn't check if a member can administrate.", guild, channel, m.Message, nil, err)
return
}
// Check
if !has {
s.ChannelMessageSend(channel.ID, "Tu n'as pas la permission de faire ça.")
return
}
prune(s, guild, channel, m.Message)
return
case "get":
// Permissions
has, err := isGuardianOrOver(s, guild, member)
if err != nil {
printDiscordError("Couldn't check if a member has a guard.", guild, channel, m.Message, nil, err)
return
}
// Check
if !has {
s.ChannelMessageSend(channel.ID, "Tu n'as pas la permission de faire ça.")
return
}
get(s, guild, channel, m.Message, command)
return
case "set":
// Permissions
has, err := canAdministrate(s, guild, member)
if err != nil {
printDiscordError("Couldn't check if a member can administrate.", guild, channel, m.Message, nil, err)
return
}
// Check
if !has {
s.ChannelMessageSend(channel.ID, "Tu n'as pas la permission de faire ça.")
return
}
set(s, guild, channel, m.Message, command)
return
case "info":
// Permissions
has, err := canModerate(s, guild, member)
if err != nil {
printDiscordError("Couldn't check if a member can moderate.", guild, channel, m.Message, nil, err)
return
}
// Check
if !has {
s.ChannelMessageSend(channel.ID, "Tu n'as pas la permission de faire ça.")
return
}
info(s, guild, channel, m.Message, command)
return
case "sar":
// Permissions
has, err := hasGuard(s, guild, member)
if err != nil {
printDiscordError("Couldn't check if a member has a guard.", guild, channel, m.Message, nil, err)
return
}
// Check
if !has {
s.ChannelMessageSend(channel.ID, "Tu n'as pas la permission de faire ça.")
return
}
sar(s, guild, channel, m.Message, command)
}
}
}
}
// Guard
done = placeInAGuard(s, guild, channel, member, m.Message)
if done {
return
}
// Nani?!
done = Nani(s, m.Message)
if done {
return
}
// Popcorn!
done = popcorn(s, channel, m.Message)
if done {
return
}
// Love!
done = love(s, guild, channel, m.Message)
if done {
return
}
}
func reactHandler(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
// Get the message structure
message, err := s.ChannelMessage(m.ChannelID, m.MessageID)
if err != nil {
fmt.Println("Couldn't get the message structure of a MessageReactionAdd!")
fmt.Println("ChannelID : " + m.ChannelID)
fmt.Println(err.Error())
return
}
// Get channel structure
channel, err := s.Channel(m.ChannelID)
if err != nil {
fmt.Println("Couldn't get the channel structure of a MessageReactionAdd!")
fmt.Println("ChannelID : " + m.ChannelID)
fmt.Println("Author : " + message.Author.Username)
fmt.Println("Message : " + message.Content)
fmt.Println(err.Error())
return
}
// Get the guild structure
guild, err := s.State.Guild(channel.GuildID)
if err != nil {
fmt.Println("Couldn't get the guild structure of a MessageReactionAdd!")
fmt.Println("Channel : " + channel.Name)
fmt.Println("Author : " + message.Author.Username)
fmt.Println("Message : " + message.Content)
fmt.Println(err.Error())
return
}
// Pin popular message
pin(s, guild, channel, message)
}
func leaveHandler(s *discordgo.Session, m *discordgo.GuildMemberRemove) {
// Don't announce it if its username contains spam.
for _, username := range censoredUsernames {
if strings.Contains(strings.ToLower(m.User.Username), username) {
return
}
}
// Get guild
guild, err := s.State.Guild(m.GuildID)
if err != nil {
fmt.Println("Couldn't get the guild of " + m.User.Username + "!")
fmt.Println(err.Error())
return
}
// Invite people who leave
waitComeBack(s, guild, m.Member)
}
func joinHandler(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
// Ban those whose username contains spam.
for _, censored := range censoredUsernames {
if strings.Contains(strings.ToLower(m.User.Username), censored) {
// Ban the bot!
err := s.GuildBanCreateWithReason(m.GuildID, m.User.ID, "Son nom d'utilisateur contenait \""+censored+"\".", 7)
if err != nil {
printDiscordError("Couldn't ban a bot spammer.", nil, nil, nil, m.User, err)
}
// Nice status
err = setStatus(s, "bannir des bots")
if err != nil {
printDiscordError("Couldn't set the status to banning bots.", nil, nil, nil, m.User, err)
}
return
}
}
// Get guild
guild, err := s.State.Guild(m.GuildID)
if err != nil {
printDiscordError("Couldn't get the guild "+m.User.Username+" just joined.", guild, nil, nil, m.User, err)
return
}
// Ask for guard
askForGuard(s, guild, m.Member.User)
}