-
Notifications
You must be signed in to change notification settings - Fork 10
/
module_prune.lua
139 lines (111 loc) · 4.17 KB
/
module_prune.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
-- 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"
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(commandMessage, targetMessage)
local channel = commandMessage.channel
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
-- This command is silent so we don't have to remove the command message here
table.remove(messagesToDelete[#messagesToDelete], #messagesToDelete[#messagesToDelete])
-- 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 = "<nbMessages>", Type = Bot.ConfigType.Integer }
},
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
})
self:RegisterCommand({
Name = "prunefrom",
Args = {
{ Name = "<messageId>", Type = Bot.ConfigType.Message }
},
PrivilegeCheck = hasManagePermission,
Help = function (guild) return Bot:Format(guild, "PRUNEFROM_HELP") end,
Silent = true,
Func = function (commandMessage, targetMessage)
local guild = commandMessage.guild
local nbDeletedMessages = self:bulkDeleteById(commandMessage, targetMessage)
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
})
return true
end