-
Notifications
You must be signed in to change notification settings - Fork 10
/
module_mute.lua
322 lines (266 loc) · 9.02 KB
/
module_mute.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
-- 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 client = Client
local discordia = Discordia
local bot = Bot
local enums = discordia.enums
local bit = require("bit")
Module.Name = "mute"
function Module:GetConfigTable()
return {
{
Array = true,
Name = "AuthorizedRoles",
Description = "Roles allowed to use mute commands",
Type = bot.ConfigType.Role,
Default = {}
},
{
Name = "DefaultMuteDuration",
Description = "Default mute duration if no duration is set",
Type = bot.ConfigType.Duration,
Default = 10 * 60
},
{
Name = "SendPrivateMessage",
Description = "Should the bot try to send a private message when muting someone?",
Type = bot.ConfigType.Boolean,
Default = true
},
{
Name = "MuteRole",
Description = "Mute role to be applied (no need to configure its permissions)",
Type = bot.ConfigType.Role,
Default = ""
}
}
end
function Module:CheckPermissions(member)
local config = self:GetConfig(member.guild)
if (util.MemberHasAnyRole(member, config.AuthorizedRoles)) then
return true
end
if (member:hasPermission(enums.permission.administrator)) then
return true
end
return false
end
function Module:OnLoaded()
self:RegisterCommand({
Name = "mute",
Args = {
{Name = "target", Type = Bot.ConfigType.Member},
{Name = "duration", Type = Bot.ConfigType.Duration, Optional = true},
{Name = "reason", Type = Bot.ConfigType.String, Optional = true},
},
PrivilegeCheck = function (member) return self:CheckPermissions(member) end,
Help = "Mutes a member",
Silent = true,
Func = function (commandMessage, targetMember, duration, reason)
local guild = commandMessage.guild
local config = self:GetConfig(guild)
local mutedBy = commandMessage.member
-- Duration
if (not duration) then
duration = config.DefaultMuteDuration
end
-- Reason
if reason and #reason > 0 then
reason = " " .. bot:Format(guild, "MUTE_REASON", reason)
else
reason = ""
end
local mutedByRole = mutedBy.highestRole
local targetRole = targetMember.highestRole
if (targetRole.position >= mutedByRole.position) then
commandMessage:reply(bot:Format(guild, "MUTE_NOTAUTHORIZED"))
return
end
if (config.SendPrivateMessage) then
local durationText
if (duration > 0) then
durationText = "\n" .. bot:Format(guild, "MUTE_YOU_WILL_BE_UNMUTED_IN", util.DiscordRelativeTime(duration))
else
durationText = ""
end
local privateChannel = targetMember:getPrivateChannel()
if (privateChannel) then
privateChannel:send(bot:Format(guild, "MUTE_PRIVATE_MESSAGE", guild.name, mutedBy.user.mentionString, reason, durationText))
end
end
local success, err = self:Mute(guild, targetMember.id, duration)
if (success) then
local durationText
if (duration > 0) then
durationText = "\n" .. bot:Format(guild, "MUTE_THEY_WILL_BE_UNMUTED_IN", util.DiscordRelativeTime(duration))
else
durationText = ""
end
commandMessage:reply(bot:Format(guild, "MUTE_GUILD_MESSAGE", mutedBy.name, targetMember.tag, reason, durationText))
else
commandMessage:reply(bot:Format(guild, "MUTE_MUTE_FAILED", targetMember.tag, err))
end
end
})
self:RegisterCommand({
Name = "unmute",
Args = {
{Name = "target", Type = Bot.ConfigType.User},
{Name = "reason", Type = Bot.ConfigType.String, Optional = true},
},
PrivilegeCheck = function (member) return self:CheckPermissions(member) end,
Help = "Unmutes a member",
Silent = true,
Func = function (commandMessage, targetUser, reason)
local guild = commandMessage.guild
local config = self:GetConfig(guild)
-- Reason
if reason and #reason > 0 then
reason = " " .. bot:Format(guild, "MUTE_REASON", reason)
else
reason = ""
end
if (config.SendPrivateMessage) then
local privateChannel = targetUser:getPrivateChannel()
if (privateChannel) then
privateChannel:send(bot:Format(guild, "MUTE_UNMUTE_MESSAGE", guild.name, commandMessage.member.mentionString, reason))
end
end
local success, err = self:Unmute(guild, targetUser.id)
if (success) then
commandMessage:reply(bot:Format(guild, "MUTE_UNMUTE_GUILD_MESSAGE", commandMessage.member.name, targetUser.tag, reason))
else
commandMessage:reply(bot:Format(guild, "MUTE_UNMUTE_FAILED", targetUser.tag, err))
end
end
})
return true
end
function Module:OnEnable(guild)
local config = self:GetConfig(guild)
local muteRole = config.MuteRole and guild:getRole(config.MuteRole) or nil
if (not muteRole) then
return false, "Invalid mute role (check your configuration)"
end
self:LogInfo(guild, "Checking mute role permission on all channels...")
for _, channel in pairs(guild.textChannels) do
self:CheckTextMutePermissions(channel)
end
for _, channel in pairs(guild.voiceChannels) do
self:CheckVoiceMutePermissions(channel)
end
local persistentData = self:GetPersistentData(guild)
persistentData.MutedUsers = persistentData.MutedUsers or {}
local data = self:GetData(guild)
data.UnmuteTimers = {}
for userId, unmuteTimestamp in pairs(persistentData.MutedUsers) do
self:RegisterUnmute(guild, userId, unmuteTimestamp)
end
return true
end
function Module:OnDisable(guild)
local data = self:GetData(guild)
if (data.UnmuteTimers) then
for userId, timer in pairs(data.UnmuteTimers) do
timer:Stop()
end
end
end
function Module:CheckTextMutePermissions(channel)
local config = self:GetConfig(channel.guild)
local mutedRole = channel.guild:getRole(config.MuteRole)
if (not mutedRole) then
self:LogError(channel.guild, "Invalid muted role")
return
end
local permissions = channel:getPermissionOverwriteFor(mutedRole)
assert(permissions)
local deniedPermissions = permissions:getDeniedPermissions()
-- :enable here just sets the bit, disabling the permissions
deniedPermissions:enable(enums.permission.addReactions, enums.permission.sendMessages, enums.permission.usePublicThreads, enums.permission.sendMessagesInThreads)
if permissions:getAllowedPermissions() ~= discordia.Permissions() or permissions:getDeniedPermissions() ~= deniedPermissions then
permissions:setPermissions('0', deniedPermissions)
end
end
function Module:CheckVoiceMutePermissions(channel)
local config = self:GetConfig(channel.guild)
local mutedRole = channel.guild:getRole(config.MuteRole)
if (not mutedRole) then
self:LogError(channel.guild, "Invalid muted role")
return
end
local permissions = channel:getPermissionOverwriteFor(mutedRole)
assert(permissions)
local deniedPermissions = permissions:getDeniedPermissions()
-- :enable here just sets the bit, disabling the permissions
deniedPermissions:enable(enums.permission.speak)
if permissions:getAllowedPermissions() ~= discordia.Permissions() or permissions:getDeniedPermissions() ~= deniedPermissions then
permissions:setPermissions('0', deniedPermissions)
end
end
function Module:Mute(guild, userId, duration)
local config = self:GetConfig(guild)
local member = guild:getMember(userId)
if (not member) then
return false, bot:Format(guild, "MUTE_ERROR_NOT_PART_OF_GUILD", "<@" .. userId .. ">")
end
local success, err = member:addRole(config.MuteRole)
if (not success) then
self:LogError(guild, "failed to mute %s: %s", member.tag, err)
return false, err
end
local persistentData = self:GetPersistentData(guild)
local unmuteTimestamp = duration > 0 and os.time() + duration or 0
persistentData.MutedUsers[userId] = unmuteTimestamp
self:RegisterUnmute(guild, userId, unmuteTimestamp)
return true
end
function Module:RegisterUnmute(guild, userId, timestamp)
if (timestamp ~= 0) then
local data = self:GetData(guild)
local timer = data.UnmuteTimers[userId]
if (timer) then
timer:Stop()
end
data.UnmuteTimers[userId] = Bot:ScheduleTimer(timestamp, function () self:Unmute(guild, userId) end)
end
end
function Module:Unmute(guild, userId)
local config = self:GetConfig(guild)
local member = guild:getMember(userId)
if (member) then
local success, err = member:removeRole(config.MuteRole)
if (not success) then
self:LogError(guild, "Failed to unmute %s: %s", member.tag, err)
return false, err
end
end
local data = self:GetData(guild)
local timer = data.UnmuteTimers[userId]
if (timer) then
timer:Stop()
data.UnmuteTimers[userId] = nil
end
local persistentData = self:GetPersistentData(guild)
persistentData.MutedUsers[userId] = nil
return true
end
function Module:OnChannelCreate(channel)
if (channel.type == enums.channelType.text) then
self:CheckTextMutePermissions(channel)
elseif (channel.type == enums.channelType.voice) then
self:CheckVoiceMutePermissions(channel)
end
end
function Module:OnMemberJoin(member)
local guild = member.guild
local config = self:GetConfig(guild)
local persistentData = self:GetPersistentData(guild)
if (persistentData.MutedUsers[member.id]) then
local success, err = member:addRole(config.MuteRole, true)
if (not success) then
self:LogError(guild, "failed to apply mute role to %s: %s", member.tag, err)
end
end
end