forked from DigitalPulseSoftware/NotaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_channels.lua
571 lines (479 loc) · 17.3 KB
/
module_channels.lua
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
-- Copyright (C) 2018 Jérôme Leclercq
-- This file is part of the "Not a Bot" application
-- For conditions of distribution and use, see copyright notice in LICENSE
local bot = Bot
local client = Client
local discordia = Discordia
local enums = discordia.enums
discordia.extensions() -- load all helpful extensions
Module.Name = "channels"
function Module:GetConfigTable()
return {
{
Name = "ReactionActions",
Description = "Map explaining which role to add/remove from which reaction on which message, use the !channelconfig command to setup this",
Type = bot.ConfigType.Custom,
Default = {}
}
}
end
function Module:OnLoaded()
self:RegisterCommand({
Name = "channelconfig",
Args = {
{Name = "configMessage", Type = Bot.ConfigType.Message, Optional = true}
},
PrivilegeCheck = function (member) return member:hasPermission(enums.permission.administrator) end,
Help = "Lists the channel module messages and reactions",
Func = function (commandMessage, configMessage)
local guild = commandMessage.guild
local config = self:GetConfig(guild)
local data = self:GetData(guild)
local channelConfig = config.ReactionActions
for channelId,messageTable in pairs(channelConfig) do
local channel = guild:getChannel(channelId)
if (channel) then
for messageId,reactionTable in pairs(messageTable) do
local message = channel:getMessage(messageId)
if (message and (not configMessage or configMessage.id == message.id)) then
local fields = {}
for emoji,actions in pairs(reactionTable) do
local actionStr = ""
if (actions.AddRoles) then
local addedRoles = {}
for _, roleId in pairs(actions.AddRoles) do
local role = guild:getRole(roleId)
if (role) then
table.insert(addedRoles, role.mentionString)
else
table.insert(addedRoles, "<invalid role " .. roleId .. ">")
end
end
if (#addedRoles > 0) then
actionStr = string.format("%s**Adds role%s** %s\n", actionStr, #addedRoles > 1 and "s" or "", table.concat(addedRoles, ", "))
end
end
if (actions.RemoveRoles) then
local removedRoles = {}
for _, roleId in pairs(actions.RemoveRoles) do
local role = guild:getRole(roleId)
if (role) then
table.insert(removedRoles, role.mentionString)
else
table.insert(removedRoles, "<invalid role " .. roleId .. ">")
end
end
if (#removedRoles > 0) then
actionStr = string.format("%s**Removes role%s** %s\n", actionStr, #removedRoles > 1 and "s" or "", table.concat(removedRoles, ", "))
end
end
if (actions.SendMessage) then
actionStr = string.format("%s**Sends private message:**\n\"%s\"\n", actionStr, actions.SendMessage)
end
local emoji = bot:GetEmojiData(guild, emoji)
table.insert(fields, {
name = string.format("- %s:", emoji and emoji.MentionString or "<invalid emoji>"),
value = actionStr
})
end
commandMessage:reply({
embed = {
description = string.format("Message in %s:\n%s", channel.mentionString, bot:GenerateMessageLink(message)),
fields = fields,
footer = {
text = "Use `!updatechannelconfig <message link> <emoji> <action>` to update channels reactions settings."
}
}
})
end
end
end
end
end
})
self:RegisterCommand({
Name = "updatechannelconfig",
Args = {
{Name = "message", Type = Bot.ConfigType.Message},
{Name = "emoji", Type = Bot.ConfigType.Emoji},
{Name = "action", Type = Bot.ConfigType.String},
{Name = "value", Type = Bot.ConfigType.String, Optional = true}
},
PrivilegeCheck = function (member) return member:hasPermission(enums.permission.administrator) end,
Help = "Configures the channel module messages and reactions",
Func = function (commandMessage, message, emoji, action, value)
local guild = commandMessage.guild
local config = self:GetConfig(guild)
local GetReactionActionsConfig = function (channelId, messageId, reaction, noCreate)
local guildConfig = config.ReactionActions
local channelTable = guildConfig[channelId]
if (not channelTable) then
if (noCreate) then
return
end
channelTable = {}
guildConfig[channelId] = channelTable
end
local messageTable = channelTable[messageId]
if (not messageTable) then
if (noCreate) then
return
end
messageTable = {}
channelTable[messageId] = messageTable
end
local reactionActions = messageTable[reaction]
if (not reactionActions) then
if (noCreate) then
return
end
reactionActions = {}
messageTable[reaction] = reactionActions
end
return reactionActions
end
local success = false
if (action == "addrole") then
local role = guild:getRole(value)
if (not role) then
commandMessage:reply(string.format("Invalid role %s", value))
return
end
local roleValue = tostring(role.id)
local reactionActions = GetReactionActionsConfig(message.channel.id, message.id, emoji.Name)
if (reactionActions.AddRoles) then
local found = false
for _, role in pairs(reactionActions.AddRoles) do
if (role == roleValue) then
found = true
break
end
end
if (reactionActions.RemoveRoles) then
for key, role in pairs(reactionActions.RemoveRoles) do
if (role == roleValue) then
table.remove(reactionActions.RemoveRoles, key)
end
end
end
if (not found) then
table.insert(reactionActions.AddRoles, tostring(role.id))
end
else
reactionActions.AddRoles = {roleValue}
end
success = true
commandMessage:reply(string.format("Reactions on %s for %s will now adds role %s (%s)", Bot:GenerateMessageLink(message), emoji.MentionString, role.name, role.id))
elseif (action == "removerole") then
local role = guild:getRole(value)
if (not role) then
commandMessage:reply(string.format("Invalid role %s", value))
return
end
local roleValue = tostring(role.id)
local reactionActions = GetReactionActionsConfig(message.channel.id, message.id, emoji.Name)
if (reactionActions.RemoveRoles) then
local found = false
for _, role in pairs(reactionActions.RemoveRoles) do
if (role == roleValue) then
found = true
break
end
end
if (reactionActions.AddRoles) then
for key, role in pairs(reactionActions.AddRoles) do
if (role == roleValue) then
table.remove(reactionActions.AddRoles, key)
end
end
end
if (not found) then
table.insert(reactionActions.RemoveRoles, tostring(role.id))
end
else
reactionActions.RemoveRoles = {roleValue}
end
success = true
commandMessage:reply(string.format("Reactions on %s for %s will now remove role %s (%s)", Bot:GenerateMessageLink(message), emoji.MentionString, role.name, role.id))
elseif (action == "send") then
if (not value) then
commandMessage:reply("Empty message")
return
end
local reactionActions = GetReactionActionsConfig(message.channel.id, message.id, emoji.Name)
reactionActions.SendMessage = value
success = true
commandMessage:reply(string.format("Reactions on %s for %s will now send private message: %s", Bot:GenerateMessageLink(message), emoji.MentionString, value))
elseif (action == "clear") then
local reactionActions = GetReactionActionsConfig(message.channel.id, message.id, emoji.Name, true)
if (reactionActions) then
reactionActions.AddRoles = nil
reactionActions.RemoveRoles = nil
reactionActions.SendMessage = nil
end
success = true
commandMessage:reply(string.format("Reactions actions on %s for %s have been cleared", Bot:GenerateMessageLink(message), emoji.MentionString))
else
commandMessage:reply("Invalid action (must be addrole/clear/removerole/send)")
end
if (success) then
self:SaveGuildConfig(guild)
commandMessage:reply(string.format("Configuration of module %s has been saved, use the `!reload %s` command to activate it", self.Name, self.Name))
end
end
})
return true
end
function Module:GetReactionActions(guild, channelId, messageId, reaction, noCreate)
local reactionKey = string.format("%s_%s_%s", channelId, messageId, reaction)
local data = self:GetData(guild)
local roleActions = data.ReactionActions[reactionKey]
if (not roleActions and not noCreate) then
roleActions = {}
roleActions.Add = {}
roleActions.Remove = {}
data.ReactionActions[reactionKey] = roleActions
end
return roleActions
end
function Module:OnEnable(guild)
local config = self:GetConfig(guild)
local data = self:GetData(guild)
data.ReactionActions = {}
--[[if (config.ChannelConfig) then
self:LogInfo(guild, "Converting old config format to new config format...")
local guildConfig = {}
for channelId,messagetable in pairs(config.ChannelConfig) do
local channelMessages = {}
guildConfig[channelId] = channelMessages
for messageId,reactionTable in pairs(messagetable) do
local messageActions = {}
channelMessages[messageId] = messageActions
for k,reactionInfo in pairs(reactionTable) do
local reactionActions = {}
messageActions[reactionInfo.reaction] = reactionActions
if (reactionInfo.roles) then
for k,roleId in pairs(reactionInfo.roles) do
if (roleId:sub(1,1) ~= "-") then
reactionActions.AddRoles = reactionActions.AddRoles or {}
table.insert(reactionActions.AddRoles, roleId)
else
roleId = roleId:sub(2)
reactionActions.RemoveRoles = reactionActions.RemoveRoles or {}
table.insert(reactionActions.RemoveRoles, roleId)
end
end
end
if (reactionInfo.message) then
reactionActions.SendMessage = reactionInfo.message
end
end
end
end
config.ReactionActions = guildConfig
config.ChannelConfig = nil
self:SaveConfig(guild)
end]]
self:LogInfo(guild, "Processing roles...")
local ProcessRole = function (channelId, messageId, reaction, roleId, remove)
if (guild:getRole(roleId)) then
local roleActions = self:GetReactionActions(guild, channelId, messageId, reaction)
if (not remove) then
table.insert(roleActions.Add, roleId)
else
table.insert(roleActions.Remove, roleId)
end
else
self:LogWarning(guild, "Role %s not found", roleId)
end
end
for channelId,messageTable in pairs(config.ReactionActions) do
for messageId,reactionTable in pairs(messageTable) do
for reaction, actions in pairs(reactionTable) do
local hasActions = false
if (actions.AddRoles) then
for _,roleId in pairs(actions.AddRoles) do
ProcessRole(channelId, messageId, reaction, roleId, false)
hasActions = true
end
end
if (actions.RemoveRoles) then
for _,roleId in pairs(actions.RemoveRoles) do
ProcessRole(channelId, messageId, reaction, roleId, true)
hasActions = true
end
end
if (actions.SendMessage) then
local roleActions = self:GetReactionActions(guild, channelId, messageId, reaction)
roleActions.Message = actions.SendMessage
hasActions = true
end
if (not hasActions) then
reactionTable[reaction] = nil
end
end
if (next(reactionTable) == nil) then
messageTable[messageId] = nil
end
end
if (next(messageTable) == nil) then
config.ReactionActions[channelId] = nil
end
end
self:LogInfo(guild, "Adding emojis to concerned messages...")
-- Make sure reactions are present on messages
for channelId,messageTable in pairs(config.ReactionActions) do
local channel = guild:getChannel(channelId)
if (channel) then
for messageId,reactionTable in pairs(messageTable) do
local message = channel:getMessage(messageId)
if (message) then
local hasReaction = {}
for k,reaction in pairs(message.reactions) do
if (reaction.me) then
local emoji = bot:GetEmojiData(guild, reaction.emojiName)
if (emoji) then
hasReaction[emoji.Name] = true
else
self:LogError(guild, "Found reaction which does not exist in guild: %s", reaction.emojiName)
end
end
end
for reaction, _ in pairs(reactionTable) do
local emoji = bot:GetEmojiData(guild, reaction)
if (emoji) then
if (not hasReaction[emoji.Name] and not message:addReaction(emoji.Emoji or emoji.Id)) then
self:LogWarning(guild, "Failed to add reaction %s on message %s (channel: %s)", emoji.Name, message.id, message.channel.id)
end
else
self:LogError(guild, "Emoji \"%s\" does not exist", reaction.emojiName)
end
end
else
self:LogError(guild, "Message %s no longer exists in channel %s", messageId, channelId)
end
end
else
self:LogError(guild, "Channel %s no longer exists", channelId)
end
end
return true
end
function Module:HandleReactionAdd(guild, userId, channelId, messageId, reactionName)
if (client.user.id == userId) then
return
end
local roleActions = self:GetReactionActions(guild, channelId, messageId, reactionName, true)
if (not roleActions) then
return
end
local member = guild:getMember(userId)
for k,roleId in pairs(roleActions.Add) do
local role = guild:getRole(roleId)
if (role) then
self:LogInfo(guild, "Adding %s%s to %s", role.name, (role.color ~= 0) and " (colored)" or "", member.tag)
if (not member:addRole(roleId)) then
self:LogWarning(guild, "Failed to add role %s to %s", role.name, member.tag)
end
else
self:LogWarning(guild, "Role %s appears to have been removed", roleId)
end
end
for k,roleId in pairs(roleActions.Remove) do
local role = guild:getRole(roleId)
if (role) then
self:LogInfo(guild, "Removing %s%s from %s", role.name, (role.color ~= 0) and " (colored)" or "", member.tag)
if (not member:removeRole(roleId)) then
self:LogWarning(guild, "Failed to remove role % from %s", role.name, member.tag)
end
else
self:LogWarning(guild, "Role %s appears to have been removed", roleId)
end
end
if (roleActions.Message) then
local privateChannel = member.user:getPrivateChannel()
if (privateChannel) then
local message = privateChannel:send(string.format("[From %s]\n%s", guild.name, roleActions.Message))
if (not message) then
self:LogWarning(guild, "Failed to send reaction message to %s (maybe user disabled private messages from this server?)", member.user.tag)
end
else
self:LogWarning(guild, "Failed to get private channel with %s", member.user.tag)
end
end
end
function Module:HandleReactionRemove(guild, userId, channelId, messageId, reactionName)
if (client.user.id == userId) then
return
end
local roleActions = self:GetReactionActions(guild, channelId, messageId, reactionName, true)
if (not roleActions) then
return
end
local member = guild:getMember(userId)
for k,roleId in pairs(roleActions.Add) do
local role = guild:getRole(roleId)
if (role) then
self:LogInfo(guild, "Removing %s%s from %s", role.name, (role.color ~= 0) and " (colored)" or "", member.tag)
if (not member:removeRole(roleId)) then
self:LogWarning(guild, "Failed to remove role %s from %s", role.name, member.tag)
end
else
self:LogWarning(guild, "Role %s appears to have been removed", roleId)
end
end
for k,roleId in pairs(roleActions.Remove) do
local role = guild:getRole(roleId)
if (role) then
self:LogInfo(guild, "Adding back %s%s to %s", role.name, (role.color ~= 0) and " (colored)" or "", member.tag)
if (not member:addRole(roleId)) then
self:LogWarning(guild, "Failed to add role %s to %s", role.name, member.tag)
end
else
self:LogWarning(guild, "Role %s appears to have been removed", roleId)
end
end
end
function Module:OnReactionAdd(reaction, userId)
if (reaction.message.channel.type ~= enums.channelType.text) then
return
end
local emoji = bot:GetEmojiData(reaction.message.guild, reaction.emojiName)
if (not emoji) then
return
end
self:HandleReactionAdd(reaction.message.channel.guild, userId, reaction.message.channel.id, reaction.message.id, emoji.Name)
end
function Module:OnReactionAddUncached(channel, messageId, reactionIdOrName, userId)
if (channel.type ~= enums.channelType.text) then
return
end
local emoji = bot:GetEmojiData(channel.guild, reactionIdOrName)
if (not emoji) then
self:LogWarning(channel.guild, "Emoji %s was used but not found in guild", reactionIdOrName)
return
end
self:HandleReactionAdd(channel.guild, userId, channel.id, messageId, emoji.Name)
end
function Module:OnReactionRemove(reaction, userId)
if (reaction.message.channel.type ~= enums.channelType.text) then
return
end
local emoji = bot:GetEmojiData(reaction.message.guild, reaction.emojiName)
if (not emoji) then
self:LogWarning(reaction.message.guild, "Emoji %s was used but not found in guild", reaction.emojiName)
return
end
self:HandleReactionRemove(reaction.message.channel.guild, userId, reaction.message.channel.id, reaction.message.id, emoji.Name)
end
function Module:OnReactionRemoveUncached(channel, messageId, reactionIdOrName, userId)
if (channel.type ~= enums.channelType.text) then
return
end
local emoji = bot:GetEmojiData(channel.guild, reactionIdOrName)
if (not emoji) then
self:LogWarning(channel.guild, "Emoji %s was used but not found in guild", reactionIdOrName)
return
end
self:HandleReactionRemove(channel.guild, userId, channel.id, messageId, emoji.Name)
end