-
Notifications
You must be signed in to change notification settings - Fork 4
/
ReactionSetupCommands.cs
474 lines (372 loc) · 17.9 KB
/
ReactionSetupCommands.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
namespace RoboBot
{
public class ReactionSetupCommands : BaseCommandModule
{
private static class CommandNames
{
public const string StartSetup = "reactsetup";
public const string FinishSetup = "reactfinish";
public const string AbortSetup = "reactabort";
public const string RestartSetup = "reactrestart";
public const string PreviewReactionMessage = "reactpreview";
public const string EditReactionMessage = "reactedit";
public const string RemoveReactionMessage = "reactremove";
public const string AddEmojiToReactionMessage = "reactadd";
public const string DeleteEmojiFromReactionMessage = "reactdel";
public const string ListReactionMessages = "reactlist";
public const string CheckReactionMessage = "reactcheck";
}
private class SetupState
{
public bool IsInSetupMode;
public bool IsInEditMode;
public ReactionMessage ReactionMessage = new ReactionMessage();
public ReactionMessage OriginalReactionMessage = new ReactionMessage();
}
private static ReactionInteractions _reactionInteractions;
private const Permissions RequiredPermissions = Permissions.Administrator;
private static Dictionary<ulong, SetupState> _setupStates = new Dictionary<ulong, SetupState>();
public static void SetReactionInteractions(ReactionInteractions reactionInteractions) => _reactionInteractions = reactionInteractions;
public static void CreateGuildState(ulong guildId) => _setupStates.Add(guildId, new SetupState());
public static void RemoveGuildState(ulong guildId) => _setupStates.Remove(guildId);
[RequireGuild]
[Command(CommandNames.StartSetup)]
public async Task StartSetup(CommandContext ctx, string messageUrl)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
SetupState state = _setupStates[ctx.Guild.Id];
if (state.IsInSetupMode || state.IsInEditMode)
{
await ctx.RespondAsync(
"The bot is already setting up / editing a reaction message, finish the previous one and try again");
return;
}
DiscordMessage message = await CommandHelpers.GetMessageFromUrl(ctx, messageUrl);
if (message is null)
return;
if (_reactionInteractions.ReactionMessages.FirstOrDefault(x => x.Message.Equals(message)) != null)
{
await ctx.RespondAsync("This message already got reaction interactions setup");
return;
}
state.ReactionMessage.Message = message;
await ctx.RespondAsync($"Got the message to use, continue the setup with {ctx.Prefix}{CommandNames.AddEmojiToReactionMessage} (emoji) (mention to role)\nPreview the rules with {ctx.Prefix}{CommandNames.PreviewReactionMessage}");
state.IsInSetupMode = true;
}
[RequireGuild]
[Command(CommandNames.StartSetup)]
public async Task StartSetup(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
await ctx.RespondAsync("You need to provide a message id to start the setup");
}
[RequireGuild]
[Command(CommandNames.EditReactionMessage)]
public async Task EditReactionMessage(CommandContext ctx, string messageUrl)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
SetupState state = _setupStates[ctx.Guild.Id];
if (state.IsInSetupMode || state.IsInEditMode)
{
await ctx.RespondAsync(
"The bot is already setting up / editing a reaction message, finish it then try again");
return;
}
DiscordMessage message = await CommandHelpers.GetMessageFromUrl(ctx, messageUrl);
if (message is null)
return;
ReactionMessage associatedMessage = _reactionInteractions.ReactionMessages.FirstOrDefault(x => x.Message.Equals(message));
if (associatedMessage == null)
{
await ctx.RespondAsync("This message doesn't have reaction interactions setup");
return;
}
state.ReactionMessage.Message = associatedMessage.Message;
foreach (KeyValuePair<DiscordEmoji, DiscordRole> rule in associatedMessage.Rules)
{
state.ReactionMessage.Rules.Add(rule.Key, rule.Value);
}
state.OriginalReactionMessage = associatedMessage;
await ctx.RespondAsync($"Got the message to edit, continue the edit with {ctx.Prefix}{CommandNames.AddEmojiToReactionMessage} (emoji) (mention to role)\nPreview the rules with {ctx.Prefix}{CommandNames.PreviewReactionMessage}");
state.IsInEditMode = true;
}
[RequireGuild]
[Command(CommandNames.EditReactionMessage)]
public async Task EditReactionMessage(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
await ctx.RespondAsync("You need to provide a message id to start the edit");
}
[RequireGuild]
[Command(CommandNames.AbortSetup)]
public async Task AbortSetup(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
SetupState state = _setupStates[ctx.Guild.Id];
if (!state.IsInSetupMode && !state.IsInEditMode)
{
await ctx.RespondAsync("Nothing to abort");
return;
}
state.ReactionMessage = new ReactionMessage();
state.OriginalReactionMessage = new ReactionMessage();
state.IsInSetupMode = false;
state.IsInEditMode = false;
await ctx.RespondAsync("Aborted setup / edit");
}
[RequireGuild]
[Command(CommandNames.RestartSetup)]
public async Task RestartSetup(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
SetupState state = _setupStates[ctx.Guild.Id];
if (!state.IsInSetupMode && !state.IsInEditMode)
{
await ctx.RespondAsync("Nothing to restart");
return;
}
if (state.IsInSetupMode)
{
state.ReactionMessage.Rules = new Dictionary<DiscordEmoji, DiscordRole>();
}
else if (state.IsInEditMode)
{
state.ReactionMessage = new ReactionMessage();
state.ReactionMessage.Message = state.OriginalReactionMessage.Message;
foreach (KeyValuePair<DiscordEmoji, DiscordRole> rule in state.OriginalReactionMessage.Rules)
{
state.ReactionMessage.Rules.Add(rule.Key, rule.Value);
}
}
await ctx.RespondAsync("Restarted setup / edit");
}
[RequireGuild]
[Command(CommandNames.AddEmojiToReactionMessage)]
public async Task AddEmojiToReactionMessage(CommandContext ctx, DiscordEmoji emoji, DiscordRole role)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
SetupState state = _setupStates[ctx.Guild.Id];
if (!state.IsInSetupMode && !state.IsInEditMode)
{
await ctx.RespondAsync("You need to have entered setup or edit mode to add reactions");
return;
}
//FIXME: This is pretty bad, try to find an other solution.
//Check to see if the bot has access to the emoji or not
try
{
await ctx.Message.CreateReactionAsync(emoji);
await ctx.Message.DeleteOwnReactionAsync(emoji);
}
catch
{
await ctx.RespondAsync("This emoji is not available to the bot");
return;
}
if (state.ReactionMessage.Rules.ContainsKey(emoji))
{
await ctx.RespondAsync("This emoji is already assigned to a role");
return;
}
state.ReactionMessage.Rules.Add(emoji, role);
}
[RequireGuild]
[Command(CommandNames.AddEmojiToReactionMessage)]
public async Task AddEmojiToReactionMessage(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
await ctx.RespondAsync("You need to provide an emoji and mention the role to attribute it to");
}
[RequireGuild]
[Command(CommandNames.AddEmojiToReactionMessage)]
public async Task AddEmojiToReactionMessage(CommandContext ctx, params string[] rest)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
await ctx.RespondAsync("You need to provide an emoji and mention the role to attribute it to");
}
[RequireGuild]
[Command(CommandNames.DeleteEmojiFromReactionMessage)]
public async Task DeleteEmojiToReactionMessage(CommandContext ctx, DiscordEmoji emoji)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
SetupState state = _setupStates[ctx.Guild.Id];
if (!state.IsInSetupMode && !state.IsInEditMode)
{
await ctx.RespondAsync("You need to have entered setup or edit mode to delete reactions from a message");
return;
}
if (!state.ReactionMessage.Rules.ContainsKey(emoji))
{
await ctx.RespondAsync("This emoji isn't associated to a role");
return;
}
state.ReactionMessage.Rules.Remove(emoji);
}
[RequireGuild]
[Command(CommandNames.DeleteEmojiFromReactionMessage)]
public async Task DeleteEmojiToReactionMessage(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
await ctx.RespondAsync("You need to provide an emoji to delete from the message");
}
[RequireGuild]
[Command(CommandNames.DeleteEmojiFromReactionMessage)]
public async Task DeleteEmojiToReactionMessage(CommandContext ctx, params string[] rest)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
await ctx.RespondAsync("You need to provide an emoji to delete from the message");
}
[RequireGuild]
[Command(CommandNames.PreviewReactionMessage)]
public async Task PreviewReactionMessage(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
SetupState state = _setupStates[ctx.Guild.Id];
if (!state.IsInSetupMode && !state.IsInEditMode)
{
await ctx.RespondAsync("You need to have entered setup or edit mode before previewing the result");
return;
}
if (state.ReactionMessage.Rules.Count == 0)
{
await ctx.RespondAsync("You need to add at least 1 reaction and role to finish the setup");
return;
}
await ctx.RespondAsync(state.ReactionMessage.ToString());
}
[RequireGuild]
[Command(CommandNames.FinishSetup)]
public async Task FinishSetup(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
SetupState state = _setupStates[ctx.Guild.Id];
if (!state.IsInSetupMode && !state.IsInEditMode)
{
await ctx.RespondAsync("You need to have entered setup or edit mode before finishing it");
return;
}
if (state.ReactionMessage.Rules.Count == 0)
{
await ctx.RespondAsync("You need to have at least 1 reaction and role to finish the setup / edit");
return;
}
if (state.IsInEditMode)
_reactionInteractions.ReactionMessages.Remove(state.OriginalReactionMessage);
_reactionInteractions.ReactionMessages.Add(state.ReactionMessage);
_reactionInteractions.SaveToFile();
string rulesString = "";
foreach (KeyValuePair<DiscordEmoji, DiscordRole> rule in state.ReactionMessage.Rules)
{
rulesString += rule.Key + " : " + rule.Value.Name + '\n';
try
{
await state.ReactionMessage.Message.CreateReactionAsync(rule.Key);
}
catch (Exception e)
{
await ctx.RespondAsync(e.ToString());
throw;
}
}
await ctx.RespondAsync(
$"Setup / edit finished!\nMessage : {state.ReactionMessage.Message.JumpLink}\nRules :\n{rulesString}");
state.ReactionMessage = new ReactionMessage();
state.OriginalReactionMessage = new ReactionMessage();
state.IsInSetupMode = false;
state.IsInEditMode = false;
}
[RequireGuild]
[Command(CommandNames.ListReactionMessages)]
public async Task ListReactionMessages(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
StringBuilder sb = new StringBuilder("Reaction Messages :");
foreach (ReactionMessage reactionMessage in _reactionInteractions.ReactionMessages.Where(x => x.Message.Channel.GuildId == ctx.Channel.GuildId))
{
sb.Append("\n\n" + reactionMessage.ToString() + '\n');
}
await ctx.RespondAsync(sb.ToString());
}
[RequireGuild]
[Command(CommandNames.RemoveReactionMessage)]
public async Task RemoveReactionMessage(CommandContext ctx, string messageToUse)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
DiscordMessage message = await CommandHelpers.GetMessageFromUrl(ctx, messageToUse);
if (message is null)
return;
ReactionMessage associatedReactionMessage =
_reactionInteractions.ReactionMessages.FirstOrDefault(x => x.Message.Equals(message));
if (associatedReactionMessage == null)
{
await ctx.RespondAsync("Couldn't find the ReactionMessage associated with this Discord message");
return;
}
_reactionInteractions.ReactionMessages.Remove(associatedReactionMessage);
await ctx.RespondAsync("Succesfully removed the corresponding ReactionMessage");
_reactionInteractions.SaveToFile();
foreach (KeyValuePair<DiscordEmoji, DiscordRole> rule in associatedReactionMessage.Rules)
{
try
{
await associatedReactionMessage.Message.DeleteOwnReactionAsync(rule.Key);
}
catch { }
}
}
[RequireGuild]
[Command(CommandNames.RemoveReactionMessage)]
public async Task RemoveReactionMessage(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
await ctx.RespondAsync("You need to provide the original message id you want to delete");
}
[RequireGuild]
[Command(CommandNames.CheckReactionMessage)]
public async Task CheckReactionMessage(CommandContext ctx, string messageToUse)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
DiscordMessage message = await CommandHelpers.GetMessageFromUrl(ctx, messageToUse);
if (message is null)
return;
ReactionMessage associatedReactionMessage =
_reactionInteractions.ReactionMessages.FirstOrDefault(x => x.Message.Equals(message));
if (associatedReactionMessage == null)
{
await ctx.RespondAsync("Couldn't find the ReactionMessage associated with this Discord message");
return;
}
await ctx.TriggerTypingAsync();
await _reactionInteractions.CheckAllRules(associatedReactionMessage);
await ctx.RespondAsync("Done");
}
}
}