-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
EventHandler.cs
412 lines (378 loc) · 16.4 KB
/
EventHandler.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Utils.Extensions;
using DSharpPlus;
using DSharpPlus.Commands;
using DSharpPlus.Commands.ContextChecks;
using DSharpPlus.Commands.EventArgs;
using DSharpPlus.Commands.Exceptions;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Exceptions;
using SupportBoi.Commands;
using SupportBoi.Interviews;
namespace SupportBoi;
public static class EventHandler
{
public static Task OnReady(DiscordClient client, GuildDownloadCompletedEventArgs e)
{
Logger.Log("Connected to Discord.");
// Checking activity type
if (!Enum.TryParse(Config.presenceType, true, out DiscordActivityType activityType))
{
Logger.Log("Presence type '" + Config.presenceType + "' invalid, using 'Playing' instead.");
activityType = DiscordActivityType.Playing;
}
client.UpdateStatusAsync(new DiscordActivity(Config.presenceText, activityType), DiscordUserStatus.Online);
return Task.CompletedTask;
}
public static async Task OnGuildAvailable(DiscordClient discordClient, GuildAvailableEventArgs e)
{
Logger.Log("Found Discord server: " + e.Guild.Name + " (" + e.Guild.Id + ")");
if (SupportBoi.commandLineArgs.serversToLeave.Contains(e.Guild.Id))
{
Logger.Warn("LEAVING DISCORD SERVER AS REQUESTED: " + e.Guild.Name + " (" + e.Guild.Id + ")");
await e.Guild.LeaveAsync();
return;
}
IReadOnlyDictionary<ulong, DiscordRole> roles = e.Guild.Roles;
foreach ((ulong roleID, DiscordRole role) in roles)
{
Logger.Debug(role.Name.PadRight(40, '.') + roleID);
}
}
public static async Task OnMessageCreated(DiscordClient client, MessageCreatedEventArgs e)
{
if (e.Author.IsBot)
{
return;
}
// Ignore messages outside of tickets.
if (!Database.TryGetOpenTicket(e.Channel.Id, out Database.Ticket ticket))
{
return;
}
// Send staff notification if applicable.
if (Config.ticketUpdatedNotifications)
{
await SendTicketUpdatedMessage(e, ticket);
}
// Try to process the message as an interview response if the ticket owner replied to this bot.
if (ticket.creatorID == e.Author.Id && e.Message.ReferencedMessage?.Author == client.CurrentUser)
{
await Interviewer.ProcessResponseMessage(e.Message);
}
}
private static async Task SendTicketUpdatedMessage(MessageCreatedEventArgs e, Database.Ticket ticket)
{
// Ignore staff messages
if (Database.IsStaff(e.Author.Id))
{
return;
}
// Sends a DM to the assigned staff member if at least a day has gone by since the last message
IReadOnlyList<DiscordMessage> messages = await e.Channel.GetMessagesAsync(2);
if (messages.Count > 1 && messages[1].Timestamp < DateTimeOffset.UtcNow.AddDays(Config.ticketUpdatedNotificationDelay * -1))
{
try
{
DiscordMember staffMember = await e.Guild.GetMemberAsync(ticket.assignedStaffID);
await staffMember.SendMessageAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = "A ticket you are assigned to has been updated: " + e.Channel.Mention
});
}
catch (NotFoundException) { }
catch (UnauthorizedException) { }
}
}
public static async Task OnMemberAdded(DiscordClient client, GuildMemberAddedEventArgs e)
{
if (!Database.TryGetOpenTickets(e.Member.Id, out List<Database.Ticket> ownTickets))
{
return;
}
foreach (Database.Ticket ticket in ownTickets)
{
try
{
DiscordChannel channel = await client.GetChannelAsync(ticket.channelID);
if (channel?.GuildId == e.Guild.Id)
{
try
{
await channel.AddOverwriteAsync(e.Member, DiscordPermissions.AccessChannels);
await channel.SendMessageAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = "User '" + e.Member.Username + "#" + e.Member.Discriminator + "' has rejoined the server, and has been re-added to the ticket."
});
}
catch (DiscordException ex)
{
Logger.Error("Exception occurred trying to add channel permissions: " + ex);
Logger.Error("JsonMessage: " + ex.JsonMessage);
}
}
}
catch (Exception) { /* ignored */ }
}
}
public static async Task OnMemberRemoved(DiscordClient client, GuildMemberRemovedEventArgs e)
{
if (Database.TryGetOpenTickets(e.Member.Id, out List<Database.Ticket> ownTickets))
{
foreach(Database.Ticket ticket in ownTickets)
{
try
{
DiscordChannel channel = await client.GetChannelAsync(ticket.channelID);
if (channel?.GuildId == e.Guild.Id)
{
await channel.SendMessageAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "User '" + e.Member.Username + "#" + e.Member.Discriminator + "' has left the server."
});
}
}
catch (Exception) { /* ignored */ }
}
}
if (Database.TryGetAssignedTickets(e.Member.Id, out List<Database.Ticket> assignedTickets) && Config.logChannel != 0)
{
DiscordChannel logChannel = await client.GetChannelAsync(Config.logChannel);
if (logChannel != null)
{
foreach (Database.Ticket ticket in assignedTickets)
{
try
{
DiscordChannel channel = await client.GetChannelAsync(ticket.channelID);
if (channel?.GuildId == e.Guild.Id)
{
await logChannel.SendMessageAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Assigned staff member '" + e.Member.Username + "#" + e.Member.Discriminator + "' has left the server: <#" + channel.Id + ">"
});
}
}
catch (Exception) { /* ignored */ }
}
}
}
}
public static async Task OnComponentInteractionCreated(DiscordClient client, ComponentInteractionCreatedEventArgs e)
{
try
{
switch (e.Interaction.Data.ComponentType)
{
case DiscordComponentType.Button:
switch (e.Id)
{
case "supportboi_closeconfirm":
await CloseCommand.OnConfirmed(e.Interaction);
return;
case not null when e.Id.StartsWith("supportboi_newcommandbutton"):
await OnNewTicketSelectorUsed(e.Interaction);
return;
case not null when e.Id.StartsWith("supportboi_newticketbutton"):
await OnNewTicketButtonUsed(e.Interaction);
return;
case not null when e.Id.StartsWith("supportboi_interviewbutton"):
await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction);
return;
case "right":
case "left":
case "rightskip":
case "leftskip":
case "stop":
return;
default:
Logger.Warn("Unknown button press received! '" + e.Id + "'");
return;
}
case DiscordComponentType.StringSelect:
switch (e.Id)
{
case not null when e.Id.StartsWith("supportboi_newcommandselector"):
await OnNewTicketSelectorUsed(e.Interaction);
return;
case not null when e.Id.StartsWith("supportboi_newticketselector"):
await CreateSelectionBoxPanelCommand.OnSelectionMenuUsed(e.Interaction);
return;
case not null when e.Id.StartsWith("supportboi_interviewselector"):
await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction);
return;
default:
Logger.Warn("Unknown selection box option received! '" + e.Id + "'");
return;
}
case DiscordComponentType.ActionRow:
Logger.Warn("Unknown action row received! '" + e.Id + "'");
return;
case DiscordComponentType.FormInput:
Logger.Warn("Unknown form input received! '" + e.Id + "'");
return;
case DiscordComponentType.UserSelect:
switch (e.Id)
{
case not null when e.Id.StartsWith("supportboi_interviewuserselector"):
await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction);
return;
default:
Logger.Warn("Unknown selection box option received! '" + e.Id + "'");
return;
}
case DiscordComponentType.RoleSelect:
switch (e.Id)
{
case not null when e.Id.StartsWith("supportboi_interviewroleselector"):
await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction);
return;
default:
Logger.Warn("Unknown selection box option received! '" + e.Id + "'");
return;
}
case DiscordComponentType.MentionableSelect:
switch (e.Id)
{
case not null when e.Id.StartsWith("supportboi_interviewmentionableselector"):
await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction);
return;
default:
Logger.Warn("Unknown selection box option received! '" + e.Id + "'");
return;
}
case DiscordComponentType.ChannelSelect:
switch (e.Id)
{
case not null when e.Id.StartsWith("supportboi_interviewchannelselector"):
await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction);
return;
default:
Logger.Warn("Unknown selection box option received! '" + e.Id + "'");
return;
}
default:
Logger.Warn("Unknown interaction type received! '" + e.Interaction.Data.ComponentType + "'");
break;
}
}
catch (DiscordException ex)
{
Logger.Error("Interaction Exception occurred: " + ex);
Logger.Error("JsomMessage: " + ex.JsonMessage);
}
catch (Exception ex)
{
Logger.Error("Interaction Exception occured: " + ex.GetType() + ": " + ex);
}
}
private static async Task OnNewTicketButtonUsed(DiscordInteraction interaction)
{
await interaction.CreateResponseAsync(DiscordInteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral());
if (!ulong.TryParse(interaction.Data.CustomId.Replace("supportboi_newticketbutton ", ""), out ulong categoryID) || categoryID == 0)
{
Logger.Warn("Invalid ticket button ID: " + interaction.Data.CustomId.Replace("supportboi_newticketbutton ", ""));
return;
}
(bool success, string message) = await NewCommand.OpenNewTicket(interaction.User.Id, interaction.ChannelId, categoryID);
if (success)
{
await interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = message
}));
}
else
{
await interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = message
}));
}
}
private static async Task OnNewTicketSelectorUsed(DiscordInteraction interaction)
{
string stringID;
switch (interaction.Data.ComponentType)
{
case DiscordComponentType.Button:
stringID = interaction.Data.CustomId.Replace("supportboi_newcommandbutton ", "");
break;
case DiscordComponentType.StringSelect:
if (interaction.Data.Values == null || interaction.Data.Values.Length <= 0)
{
return;
}
stringID = interaction.Data.Values[0];
break;
case DiscordComponentType.ActionRow:
case DiscordComponentType.FormInput:
default:
return;
}
if (!ulong.TryParse(stringID, out ulong categoryID) || categoryID == 0)
{
return;
}
await interaction.CreateResponseAsync(DiscordInteractionResponseType.DeferredMessageUpdate, new DiscordInteractionResponseBuilder().AsEphemeral());
(bool success, string message) = await NewCommand.OpenNewTicket(interaction.User.Id, interaction.ChannelId, categoryID);
if (success)
{
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = message
}));
}
else
{
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = message
}));
}
}
public static async Task OnCommandError(CommandsExtension commandSystem, CommandErroredEventArgs e)
{
switch (e.Exception)
{
case ChecksFailedException checksFailedException:
{
foreach (ContextCheckFailedData error in checksFailedException.Errors)
{
await e.Context.Channel.SendMessageAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = error.ErrorMessage
});
}
return;
}
case BadRequestException ex:
Logger.Error("Command exception occured:\n" + e.Exception);
Logger.Error("JSON Message: " + ex.JsonMessage);
return;
default:
{
Logger.Error("Exception occured: " + e.Exception.GetType() + ": " + e.Exception);
await e.Context.Channel.SendMessageAsync(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Internal error occured, please report this to the developer."
});
return;
}
}
}
}