-
Notifications
You must be signed in to change notification settings - Fork 10
/
module_purge.lua
221 lines (172 loc) · 6.2 KB
/
module_purge.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
-- 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 = "purge"
function Module:OnLoaded()
self:RegisterCommand({
Name = "drypurge",
Args = {
{Name = "time", Type = bot.ConfigType.Duration}
},
PrivilegeCheck = function (member) return member:hasPermission(enums.permission.administrator) end,
Help = "Count inactive people. Use: !drypurgeroles 30d to count inactive people for 30 days or more.",
Func = function (commandMessage, time)
local durationStr = util.FormatTime(time, 3)
local userList = self:BuildInactiveUsersList(commandMessage.guild, time)
if table.empty(userList) then
commandMessage:reply("No user enought inactive to be purged")
else
commandMessage:reply("Purging peoples inactive for " .. durationStr .. " on Discord will remove all roles on (or kick) ".. table.length(userList) .." members. Are you sure ? (type !purgeroles " .. time .. " to apply purge by roles, or !purgekick " .. time .. " to kick all inactives members.)")
end
end
})
self:RegisterCommand({
Name = "purgeroles",
Args = {
{Name = "time", Type = bot.ConfigType.Duration}
},
PrivilegeCheck = function (member) return member:hasPermission(enums.permission.administrator) end,
Help = "Clear roles on inactive people to make them avaiable to purge. Use: !purgeroles 30d to remove all roles on members inactive for 30 days or more.",
Func = function (commandMessage, time)
local durationStr = util.FormatTime(time, 3)
local userList = self:BuildInactiveUsersList(commandMessage.guild, time)
if #userList == 0 then
commandMessage:reply("No member to purge")
else
self:PurgeRoles(commandMessage.guild, userList)
commandMessage:reply("Purged peoples inactive for " .. durationStr .. " on Discord, removed all roles on ".. #userList .." members.")
end
end
})
self:RegisterCommand({
Name = "purgekick",
Args = {
{Name = "time", Type = bot.ConfigType.Duration}
},
PrivilegeCheck = function (member) return member:hasPermission(enums.permission.administrator) end,
Help = "Kick inactive people. Use: !purgekick 30d to kick all inactive people for 30 days or more.",
Func = function (commandMessage, time)
local durationStr = util.FormatTime(time, 3)
local userList = self:BuildInactiveUsersList(commandMessage.guild, time)
if #userList == 0 then
commandMessage:reply("No member to purge")
else
self:PurgeKick(commandMessage.guild, userList, durationStr)
commandMessage:reply("Purged peoples inactive for " .. durationStr .. " on this server, kicked ".. #userList .." members.")
end
end
})
return true
end
function Module:OnEnable(guild)
local data = self:GetPersistentData(guild)
if (not data.Purge) then
self:LogInfo(guild, "No previous purge data found, resetting...")
data.Purge = {}
else
self:LogInfo(guild, "Previous purge data data has been found, continuing...")
end
self:AddMissingMembersToList(guild)
return true
end
function Module:BuildInactiveUsersList(guild, time)
local userList = {}
local persistentData = self:GetPersistentData(guild)
local purgeData = persistentData.Purge
local allowedInactiveTime = os.time() - time
for userId, user in pairs(guild.members) do
if purgeData[userId] ~= nil then
if purgeData[userId] < allowedInactiveTime then
table.insert(userList, user)
end
end
end
return userList
end
function Module:PurgeRoles(guild, userList)
if #userList == 0 then
return
end
self:LogInfo(guild, "Begining role purge")
for _, user in pairs(userList) do
self:LogInfo(guild, "Purging roles for %s", user.name)
for _, role in pairs(user.roles) do
if not role.managed then -- You can't remove managed roles, they are for example unique bot roles, discord will send a 403 if you try
user:removeRole(role.id)
end
end
end
self:LogInfo(guild, "Role purge ended !")
end
function Module:PurgeKick(guild, userList, durationStr)
if table.empty(userList) then
return
end
local kickPrivateMessage = string.format("You've been kicked by an automatic measure from **%s** because of an inactivity of **%s** or more.", guild.name, durationStr)
self:LogInfo(guild, "Begining purge")
for _, user in pairs(userList) do
self:LogInfo(guild, "Kicking %s", user.name)
local privateChannel = user:getPrivateChannel()
if (privateChannel) then
privateChannel:send(kickPrivateMessage)
end
user:kick("Inactive")
end
self:LogInfo(guild, "Purge ended !")
end
function Module:AddMissingMembersToList(guild)
local persistentData = self:GetPersistentData(guild)
local purgeData = persistentData.Purge
for userId, userData in pairs(guild.members) do
if purgeData[userId] == nil then
purgeData[userId] = os.time()
end
end
end
function Module:OnMessageCreate(message)
if (not bot:IsPublicChannel(message.channel)) then
return
end
local data = self:GetPersistentData(message.guild)
data.Purge[message.author.id] = os.time()
end
function Module:OnMemberJoin(member)
local data = self:GetPersistentData(member.guild)
data.Purge[member.user.id] = os.time()
end
function Module:OnReactionAdd(reaction, userId)
if (not bot:IsPublicChannel(reaction.message.channel)) then
return
end
local data = self:GetPersistentData(reaction.message.guild)
data.Purge[userId] = os.time()
end
function Module:OnReactionAddUncached(channel, messageId, reactionIdorName, userId)
if (not bot:IsPublicChannel(channel)) then
return
end
local data = self:GetPersistentData(channel.guild)
data.Purge[userId] = os.time()
end
function Module:OnReactionRemove(reaction, userId)
if (not bot:IsPublicChannel(reaction.message.channel)) then
return
end
local data = self:GetPersistentData(reaction.message.guild)
data.Purge[userId] = os.time()
end
function Module:OnReactionRemoveUncached(channel, messageId, reactionIdorName, userId)
if (not bot:IsPublicChannel(channel)) then
return
end
local data = self:GetPersistentData(channel.guild)
data.Purge[userId] = os.time()
end