-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmodule_prune.lua
More file actions
193 lines (157 loc) · 5.93 KB
/
Copy pathmodule_prune.lua
File metadata and controls
193 lines (157 loc) · 5.93 KB
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
-- Copyright (C) 2023 Mjöllnir#3515
-- This file is part of the "Not a Bot" application
-- For conditions of distribution and use, see copyright notice in LICENSE
-- API limitations on bulk deletion:
-- It is not possible to delete less than 2 messages or more than 100 messages at a time
-- It is not possible to delete messages older than 14 days
-- getMessagesBefore and getMessagesAfter have also a limitation to 100 messages at a time
local Date = Discordia.Date
local Enums = Discordia.enums
local MSG_TIME_LIMIT = 1209600 -- seconds
local NB_MSG_MAX_LIMIT = 100
Module.Name = "prune"
function Module:GetConfigTable()
return {
{
Name = "Silent",
Description = "Delete the command message used to invoke prunefrom",
Type = Bot.ConfigType.Boolean,
Default = true
}
}
end
local function bulkDeleteChunks(channel, messagesChunks)
local nbDeletedMessages = 0
for _, chunk in ipairs(messagesChunks) do
if #chunk > 1 then
channel:bulkDelete(chunk)
nbDeletedMessages = nbDeletedMessages + #chunk
else
chunk[1]:delete()
nbDeletedMessages = nbDeletedMessages + 1
end
end
return nbDeletedMessages
end
local function hasValidDate(message)
local messageTimestamp = Date.fromSnowflake(message.id):toSeconds()
return os.difftime(os.time(), messageTimestamp) < MSG_TIME_LIMIT
end
local function hasManagePermission(member)
return member:hasPermission(Enums.permission.manageMessages)
end
function Module:bulkDeleteByNumber(commandMessage, nbMessages)
local channel = commandMessage.channel
local currentMessageId = commandMessage.id
local messagesToDelete = {}
local quotient = math.floor(nbMessages / NB_MSG_MAX_LIMIT)
local remainder = nbMessages - quotient * NB_MSG_MAX_LIMIT
for _ = 1, quotient do
local messages = channel:getMessagesBefore(currentMessageId, NB_MSG_MAX_LIMIT):toArray("id", hasValidDate)
if next(messages) ~= nil then
table.insert(messagesToDelete, messages)
currentMessageId = messages[1].id
end
end
if remainder > 0 then
table.insert(
messagesToDelete, channel:getMessagesBefore(currentMessageId, remainder):toArray("id", hasValidDate)
)
end
return bulkDeleteChunks(channel, messagesToDelete)
end
function Module:bulkDeleteById(channel, targetMessage, trimInvokingMessage)
local currentMessageId = targetMessage.id
local messagesToDelete = {}
local messages
repeat
messages = channel:getMessagesAfter(currentMessageId, NB_MSG_MAX_LIMIT):toArray("id", hasValidDate)
if next(messages) ~= nil then
table.insert(messagesToDelete, messages)
currentMessageId = messages[#messages].id
end
until next(messages) == nil
-- Text command invocation posts a message right after the range we just fetched; strip it.
-- Context-menu invocations don't post one, so this must stay conditional.
if (trimInvokingMessage) then
table.remove(messagesToDelete[#messagesToDelete], #messagesToDelete[#messagesToDelete])
end
-- Delete also the selected message
if hasValidDate(targetMessage) then
table.insert(messagesToDelete[#messagesToDelete], targetMessage)
end
return bulkDeleteChunks(channel, messagesToDelete)
end
function Module:OnLoaded()
self:RegisterCommand({
Name = "prune",
Args = {
{ Name = "nbOfMessages", Type = Bot.ConfigType.Integer, Description = "Number of recent messages to delete" }
},
PrivilegeCheck = hasManagePermission,
Help = function (guild) return Bot:Format(guild, "PRUNE_HELP") end,
Silent = true,
Func = function (commandMessage, nbMessages)
local guild = commandMessage.guild
local nbDeletedMessages = self:bulkDeleteByNumber(commandMessage, nbMessages)
local response = ""
if nbDeletedMessages ~= nbMessages then
response = string.format("%s\n", Bot:Format(guild, "PRUNE_CANNOT_DELETE"))
end
response = response .. Bot:Format(guild, "PRUNE_RESULT", nbDeletedMessages)
return commandMessage:reply(response)
end,
Slash = {
Description = "Delete a number of recent messages in this channel",
DefaultMemberPermissions = Enums.permission.manageMessages,
Func = function (interaction, nbMessages)
local guild = interaction.guild
interaction:respond({ type = Enums.interactionResponseType.deferredChannelMessageWithSource })
local nbDeletedMessages = self:bulkDeleteByNumber(interaction, nbMessages)
local response = ""
if nbDeletedMessages ~= nbMessages then
response = string.format("%s\n", Bot:Format(guild, "PRUNE_CANNOT_DELETE"))
end
response = response .. Bot:Format(guild, "PRUNE_RESULT", nbDeletedMessages)
interaction:editResponse({ content = response })
end
}
})
self:RegisterCommand({
Name = "prunefrom",
Args = {
{ Name = "messageId", Type = Bot.ConfigType.Message }
},
PrivilegeCheck = hasManagePermission,
Help = function (guild) return Bot:Format(guild, "PRUNEFROM_HELP") end,
Func = function (commandMessage, targetMessage)
local guild = commandMessage.guild
local config = self:GetConfig(guild)
local nbDeletedMessages = self:bulkDeleteById(commandMessage.channel, targetMessage, config.Silent)
local response = ""
if not hasValidDate(targetMessage) then
response = string.format("%s\n", Bot:Format(guild, "PRUNE_CANNOT_DELETE"))
end
response = response .. Bot:Format(guild, "PRUNE_RESULT", nbDeletedMessages)
return commandMessage:reply(response)
end,
ContextMenu = {
Type = "message",
DefaultMemberPermissions = Enums.permission.manageMessages,
Func = function (interaction, targetMessage)
local guild = interaction.guild
interaction:respond({
type = Enums.interactionResponseType.deferredChannelMessageWithSource
})
local nbDeletedMessages = self:bulkDeleteById(interaction.channel, targetMessage, false)
local response = ""
if not hasValidDate(targetMessage) then
response = string.format("%s\n", Bot:Format(guild, "PRUNE_CANNOT_DELETE"))
end
response = response .. Bot:Format(guild, "PRUNE_RESULT", nbDeletedMessages)
interaction:editResponse({ content = response })
end
}
})
return true
end