forked from DigitalPulseSoftware/NotaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_nickname.lua
285 lines (241 loc) · 6.98 KB
/
module_nickname.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
-- Copyright (C) 2019-2020 Axel "Elanis" Soupé
-- 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 config = Config
local discordia = Discordia
local enums = discordia.enums
local fs = require("coro-fs")
local json = require("json")
local path = require("path")
Module.Name = "nickname"
Module.PageSize = 3
function Module:OnLoaded()
self:RegisterCommand({
Name = "drynukerename",
Args = {},
PrivilegeCheck = function (member) return self:CheckRoles(member) end,
Help = "Count every user that have a custom nickname on the server.",
Func = function (commandMessage, time)
local userList = self:BuildRenamedUserList(commandMessage.guild)
if #userList == 0 then
commandMessage:reply("No renamed user")
else
commandMessage:reply("This will remove custom nickname for ".. #userList .." members. Are you sure ? Type !nukerename to apply.")
end
end
})
self:RegisterCommand({
Name = "nukerename",
Args = {},
PrivilegeCheck = function (member) return self:CheckRoles(member) end,
Help = "Remove every custom nickname of every user on the server.",
Func = function (commandMessage, time)
local userList = self:BuildRenamedUserList(commandMessage.guild)
if #userList == 0 then
commandMessage:reply("No renamed user")
else
self:RemoveNicknames(commandMessage.guild, commandMessage.member, userList)
commandMessage:reply("Removing custom nickname for ".. #userList .." members.")
end
end
})
self:RegisterCommand({
Name = "managenicknames",
Args = {},
PrivilegeCheck = function (member) return self:CheckRoles(member) end,
Help = "Display user nicknames and a button to reset them.",
Func = function (commandMessage, time)
local userList = self:BuildRenamedUserList(commandMessage.guild)
if #userList == 0 then
commandMessage:reply("No renamed user")
else
commandMessage:reply(self:BuildUserListMessage(commandMessage.guild, userList, 0))
end
end
})
return true
end
function Module:OnEnable(guild)
return true
end
function Module:CheckRoles(member)
return member:hasPermission(enums.permission.manageNicknames)
end
function Module:HasHighestRolesThanTarget(user, target)
local bannedByRole = user.highestRole
local targetRole = target.highestRole
if (targetRole.position >= bannedByRole.position) then
return false
end
return true
end
function Module:OnInteractionCreate(interaction)
local guild = interaction.guild
if not guild then
return
end
local member = interaction.member
if not self:CheckRoles(member) then
return
end
local config = self:GetConfig(guild)
local interactionType = interaction.data.custom_id
local custom_id_start_remove = 'nickname_remove_'
local custom_id_start_page = 'nickname_page_'
local userList = self:BuildRenamedUserList(guild)
if #userList == 0 then
interaction:respond({
type = enums.interactionResponseType.channelMessageWithSource,
data = {
content = "No renamed user",
flags = enums.interactionResponseFlag.ephemeral
}
});
return
end
if interactionType:startswith(custom_id_start_remove) then
local cmdUserId = string.sub(interactionType, string.len(custom_id_start_remove) + 1, string.len(interactionType))
for _, user in pairs(userList) do
if cmdUserId == user.id then
if not self:HasHighestRolesThanTarget(interaction.member, user) then
interaction:respond({
type = enums.interactionResponseType.channelMessageWithSource,
data = {
content = "You cannot rename that user due to your lower permissions.",
flags = enums.interactionResponseFlag.ephemeral
}
});
return
end
user:setNickname('')
break
end
end
interaction:respond({
type = enums.interactionResponseType.channelMessageWithSource,
data = {
content = "Done !",
flags = enums.interactionResponseFlag.ephemeral
}
})
elseif interactionType:startswith(custom_id_start_page) then
local page = string.sub(interactionType, string.len(custom_id_start_page) + 1, string.len(interactionType))
interaction.message:update(self:BuildUserListMessage(guild, userList, tonumber(page)))
interaction:respond({
type = enums.interactionResponseType.updateMessage,
})
end
end
function Module:BuildRenamedUserList(guild)
local userList = {}
for userId, user in pairs(guild.members) do
if user.nickname then
table.insert(userList, user)
end
end
return userList
end
function Module:RemoveNicknames(guild, currentUser, userList)
if #userList == 0 then
return
end
self:LogInfo(guild, "Begining nickname nuke ...")
for _, user in pairs(userList) do
self:LogInfo(guild, "Renaming %s", user.name)
if self:HasHighestRolesThanTarget(currentUser, user) then
user:setNickname('')
return
end
end
self:LogInfo(guild, "Nickname nuke ended !")
end
function Module:BuildUserListMessage(guild, userList, currentPage)
local components = {}
table.sort(userList, function(a, b) return a.username < b.username end)
table.insert(components, {
type = enums.componentType.actionRow,
components = {
{
style = enums.buttonStyle.secondary,
label = "Real Name",
custom_id = "row_header_button_0",
disabled = true,
type = enums.componentType.button
},
{
style = enums.buttonStyle.secondary,
label = "Custom Name",
custom_id = "row_header_button_1",
disabled = true,
type = enums.componentType.button
},
}
})
local i = 0
for _, user in pairs(userList) do
if i >= Module.PageSize * currentPage then
if #components > Module.PageSize then
break
end
table.insert(components, {
type = enums.componentType.actionRow,
components = {
{
style = enums.buttonStyle.secondary,
label = user._user._username,
custom_id = "row_" .. #components .. "_button_0",
disabled = true,
type = enums.componentType.button
},
{
style = enums.buttonStyle.secondary,
label = user.nickname,
custom_id = "row_" .. #components .. "_button_1",
disabled = true,
type = enums.componentType.button
},
{
style = enums.buttonStyle.primary,
label = "Remove custom username",
custom_id = "nickname_remove_" .. user.id,
disabled = false,
type = enums.componentType.button
}
}
})
end
i = i+1
end
table.insert(components, {
type = enums.componentType.actionRow,
components = {
{
style = enums.buttonStyle.primary,
label = "Prev Page",
custom_id = "nickname_page_" .. currentPage - 1,
disabled = currentPage == 0,
type = enums.componentType.button
},
{
style = enums.buttonStyle.primary,
label = "Next Page",
custom_id = "nickname_page_" .. currentPage + 1,
disabled = #userList <= Module.PageSize * (currentPage + 1),
type = enums.componentType.button
},
}
})
return {
components = components,
embeds = {
{
type = "rich",
title = "Test",
description = "",
color = 0x00FFFF
}
}
}
end