-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathClearInteractions.cs
295 lines (264 loc) · 13.9 KB
/
ClearInteractions.cs
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
namespace Cliptok.Commands.InteractionCommands
{
public class ClearInteractions : ApplicationCommandModule
{
public static Dictionary<ulong, List<DiscordMessage>> MessagesToClear = new();
[SlashCommand("clear", "Delete many messages from the current channel.", defaultPermission: false)]
[HomeServer, SlashRequireHomeserverPerm(ServerPermLevel.TrialModerator), RequireBotPermissions(permissions: [DiscordPermission.ManageMessages, DiscordPermission.ModerateMembers])]
public async Task ClearSlashCommand(InteractionContext ctx,
[Option("count", "The number of messages to consider for deletion. Required if you don't use the 'up_to' argument.")] long count = 0,
[Option("up_to", "Optionally delete messages up to (not including) this one. Accepts IDs and links.")] string upTo = "",
[Option("user", "Optionally filter the deletion to a specific user.")] DiscordUser user = default,
[Option("ignore_mods", "Optionally filter the deletion to only messages sent by users who are not Moderators.")] bool ignoreMods = false,
[Option("match", "Optionally filter the deletion to only messages containing certain text.")] string match = "",
[Option("bots_only", "Optionally filter the deletion to only bots.")] bool botsOnly = false,
[Option("humans_only", "Optionally filter the deletion to only humans.")] bool humansOnly = false,
[Option("attachments_only", "Optionally filter the deletion to only messages with attachments.")] bool attachmentsOnly = false,
[Option("stickers_only", "Optionally filter the deletion to only messages with stickers.")] bool stickersOnly = false,
[Option("links_only", "Optionally filter the deletion to only messages containing links.")] bool linksOnly = false,
[Option("dry_run", "Don't actually delete the messages, just output what would be deleted.")] bool dryRun = false
)
{
await ctx.DeferAsync(ephemeral: !dryRun);
// If all args are unset
if (count == 0 && upTo == "" && user == default && ignoreMods == false && match == "" && botsOnly == false && humansOnly == false && attachmentsOnly == false && stickersOnly == false && linksOnly == false)
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} You must provide at least one argument! I need to know which messages to delete.").AsEphemeral(true));
return;
}
if (count == 0 && upTo == "")
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} I need to know how many messages to delete! Please provide a value for `count` or `up_to`.").AsEphemeral(true));
return;
}
// If count is too low or too high, refuse the request
if (count < 0)
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} I can't delete a negative number of messages! Try setting `count` to a positive number.").AsEphemeral(true));
return;
}
if (count >= 1000)
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} Deleting that many messages poses a risk of something disastrous happening, so I'm refusing your request, sorry.").AsEphemeral(true));
return;
}
// Get messages to delete, whether that's messages up to a certain one or the last 'x' number of messages.
if (upTo != "" && count != 0)
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} You can't provide both a count of messages and a message to delete up to! Please only provide one of the two arguments.").AsEphemeral(true));
return;
}
List<DiscordMessage> messagesToClear = new();
if (upTo == "")
{
var messages = await ctx.Channel.GetMessagesAsync((int)count).ToListAsync();
messagesToClear = messages.ToList();
}
else
{
DiscordMessage message;
ulong messageId;
if (!upTo.Contains("discord.com"))
{
if (!ulong.TryParse(upTo, out messageId))
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} That doesn't look like a valid message ID or link! Please try again."));
return;
}
}
else
{
if (
Constants.RegexConstants.discord_link_rx.Match(upTo).Groups[2].Value != ctx.Channel.Id.ToString()
|| !ulong.TryParse(Constants.RegexConstants.discord_link_rx.Match(upTo).Groups[3].Value, out messageId)
)
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} Please provide a valid link to a message in this channel!").AsEphemeral(true));
return;
}
}
// This is the message we will delete up to. This message will not be deleted.
message = await ctx.Channel.GetMessageAsync(messageId);
// List of messages to delete, up to (not including) the one we just got.
var firstMsg = (await ctx.Channel.GetMessagesAfterAsync(message.Id, 1).ToListAsync())[0];
var firstMsgId = firstMsg.Id;
messagesToClear.Add(firstMsg);
while (true)
{
var newMessages = (await ctx.Channel.GetMessagesAfterAsync(firstMsgId, 100).ToListAsync()).ToList();
messagesToClear.AddRange(newMessages);
firstMsgId = newMessages.First().Id;
if (newMessages.Count() < 100)
break;
}
}
// Now we know how many messages we'll be looking through and we won't be refusing the request. Time to check filters.
// Order of priority here is the order of the arguments for the command.
// Match user
if (user != default)
{
foreach (var message in messagesToClear.ToList())
{
if (message.Author.Id != user.Id)
{
messagesToClear.Remove(message);
}
}
}
// Ignore mods
if (ignoreMods)
{
foreach (var message in messagesToClear.ToList())
{
DiscordMember member;
try
{
member = await ctx.Guild.GetMemberAsync(message.Author.Id);
}
catch (DSharpPlus.Exceptions.NotFoundException)
{
// User is not in the server, so they can't be a current mod
continue;
}
if ((await GetPermLevelAsync(member)) >= ServerPermLevel.TrialModerator)
{
messagesToClear.Remove(message);
}
}
}
// Match text
if (match != "")
{
foreach (var message in messagesToClear.ToList())
{
if (!message.Content.ToLower().Contains(match.ToLower()))
{
messagesToClear.Remove(message);
}
}
}
// Bots only
if (botsOnly)
{
if (humansOnly)
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} You can't use `bots_only` and `humans_only` together! Pick one or the other please.").AsEphemeral(true));
return;
}
foreach (var message in messagesToClear.ToList())
{
if (!message.Author.IsBot)
{
messagesToClear.Remove(message);
}
}
}
// Humans only
if (humansOnly)
{
foreach (var message in messagesToClear.ToList())
{
if (message.Author.IsBot)
{
messagesToClear.Remove(message);
}
}
}
// Attachments only
if (attachmentsOnly)
{
foreach (var message in messagesToClear.ToList())
{
if (message.Attachments.Count == 0)
{
messagesToClear.Remove(message);
}
}
}
// Stickers only
if (stickersOnly)
{
foreach (var message in messagesToClear.ToList())
{
if (message.Stickers.Count == 0)
{
messagesToClear.Remove(message);
}
}
}
// Links only
if (linksOnly)
{
foreach (var message in messagesToClear.ToList())
{
if (!Constants.RegexConstants.url_rx.IsMatch(message.Content.ToLower()))
{
messagesToClear.Remove(message);
}
}
}
// Skip messages older than 2 weeks, since Discord won't let us delete them anyway
bool skipped = false;
foreach (var message in messagesToClear.ToList())
{
if (message.CreationTimestamp.ToUniversalTime() < DateTime.UtcNow.AddDays(-14))
{
messagesToClear.Remove(message);
skipped = true;
}
}
if (messagesToClear.Count == 0 && skipped)
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} All of the messages to delete are older than 2 weeks, so I can't delete them!").AsEphemeral(true));
return;
}
// All filters checked. 'messages' is now our final list of messages to delete.
if (dryRun)
{
var msg = await LogChannelHelper.CreateDumpMessageAsync($"{Program.cfgjson.Emoji.Information} **{messagesToClear.Count}** messages would have been deleted, but are instead logged below.",
messagesToClear,
ctx.Channel);
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent(msg.Content).AddFiles(msg.Files).AddEmbeds(msg.Embeds).AsEphemeral(false));
return;
}
// Warn the mod if we're going to be deleting 50 or more messages.
if (messagesToClear.Count >= 50)
{
DiscordButtonComponent confirmButton = new(DiscordButtonStyle.Danger, "clear-confirm-callback", "Delete Messages");
DiscordMessage confirmationMessage = await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Muted} You're about to delete {messagesToClear.Count} messages. Are you sure?").AddComponents(confirmButton).AsEphemeral(true));
MessagesToClear.Add(confirmationMessage.Id, messagesToClear);
}
else
{
if (messagesToClear.Count >= 1)
{
await ctx.Channel.DeleteMessagesAsync(messagesToClear, $"[Clear by {DiscordHelpers.UniqueUsername(ctx.User)}]");
if (skipped)
{
await ctx.Channel.SendMessageAsync($"{Program.cfgjson.Emoji.Deleted} Cleared **{messagesToClear.Count}** messages from {ctx.Channel.Mention}!\nSome messages were not deleted because they are older than 2 weeks.");
}
else
{
await ctx.Channel.SendMessageAsync($"{Program.cfgjson.Emoji.Deleted} Cleared **{messagesToClear.Count}** messages from {ctx.Channel.Mention}!");
}
await LogChannelHelper.LogMessageAsync("mod",
new DiscordMessageBuilder()
.WithContent($"{Program.cfgjson.Emoji.Deleted} **{messagesToClear.Count}** messages were cleared in {ctx.Channel.Mention} by {ctx.User.Mention}.")
.WithAllowedMentions(Mentions.None)
);
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Success} Done!").AsEphemeral(true));
}
else
{
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent($"{Program.cfgjson.Emoji.Error} There were no messages that matched all of the arguments you provided! Nothing to do."));
}
await LogChannelHelper.LogDeletedMessagesAsync(
"messages",
$"{Program.cfgjson.Emoji.Deleted} **{messagesToClear.Count}** messages were cleared from {ctx.Channel.Mention} by {ctx.User.Mention}.",
messagesToClear,
ctx.Channel
);
}
}
}
}