-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventHandler.cs
167 lines (151 loc) · 5.82 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
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Exceptions;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using DSharpPlus.SlashCommands.EventArgs;
namespace RoleManager
{
internal static class EventHandler
{
internal static Task OnReady(DiscordClient client, ReadyEventArgs e)
{
Logger.Log(LogID.DISCORD, "Client is ready to process events.");
// Checking activity type
if (!Enum.TryParse(Config.presenceType, true, out ActivityType activityType))
{
Logger.Log(LogID.CONFIG, "Presence type '" + Config.presenceType + "' invalid, using 'Playing' instead.");
activityType = ActivityType.Playing;
}
client.UpdateStatusAsync(new DiscordActivity(Config.presenceText, activityType), UserStatus.Online);
return Task.CompletedTask;
}
internal static Task OnGuildAvailable(DiscordClient _, GuildCreateEventArgs e)
{
Logger.Log(LogID.DISCORD, "Guild available: " + e.Guild.Name);
IReadOnlyDictionary<ulong, DiscordRole> roles = e.Guild.Roles;
foreach ((ulong roleID, DiscordRole role) in roles)
{
Logger.Log(LogID.DISCORD, role.Name.PadRight(40, '.') + roleID);
}
return Task.CompletedTask;
}
internal static Task OnClientError(DiscordClient _, ClientErrorEventArgs e)
{
Logger.Error(LogID.DISCORD, "Exception occured:\n" + e.Exception);
return Task.CompletedTask;
}
internal static Task OnCommandError(SlashCommandsExtension commandSystem, SlashCommandErrorEventArgs e)
{
switch (e.Exception)
{
case SlashExecutionChecksFailedException checksFailedException:
{
foreach (SlashCheckBaseAttribute attr in checksFailedException.FailedChecks)
{
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = ParseFailedCheck(attr)
};
e.Context.CreateResponseAsync(error);
}
return Task.CompletedTask;
}
default:
{
Logger.Error(LogID.COMMAND, "Exception occured: " + e.Exception.GetType() + ": " + e.Exception);
if (e.Exception is UnauthorizedException ex)
{
Logger.Error(LogID.DISCORD, ex.WebResponse.Response);
}
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Internal error occured, please report this to the developer."
};
e.Context.CreateResponseAsync(error);
return Task.CompletedTask;
}
}
}
internal static async Task OnComponentInteractionCreated(DiscordClient client, ComponentInteractionCreateEventArgs e)
{
try
{
switch (e.Interaction.Data.ComponentType)
{
case ComponentType.StringSelect:
if (!e.Interaction.Data.CustomId.StartsWith("rolemanager_togglerole"))
{
return;
}
if (e.Interaction.Data.Values.Length == 0)
{
await e.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, new DiscordInteractionResponseBuilder().WithContent(e.Message.Content).AddComponents(e.Message.Components));
}
foreach (string stringID in e.Interaction.Data.Values)
{
if (!ulong.TryParse(stringID, out ulong roleID) || roleID == 0) continue;
DiscordMember member = await e.Guild.GetMemberAsync(e.User.Id);
if (!e.Guild.Roles.ContainsKey(roleID) || member == null) continue;
if (member.Roles.Any(role => role.Id == roleID))
{
await member.RevokeRoleAsync(e.Guild.Roles[roleID]);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = "Revoked role " + e.Guild.Roles[roleID].Mention + "!"
}).AsEphemeral());
}
else
{
await member.GrantRoleAsync(e.Guild.Roles[roleID]);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
Description = "Granted role " + e.Guild.Roles[roleID].Mention + "!"
}).AsEphemeral());
}
}
break;
case ComponentType.ActionRow:
case ComponentType.Button:
case ComponentType.FormInput:
return;
}
}
catch (UnauthorizedException ex)
{
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "The bot doesn't have the required permissions to do that!"
}).AsEphemeral());
}
catch (Exception ex)
{
Logger.Error(LogID.COMMAND, "Exception occured: " + ex.GetType() + ": " + ex);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AddEmbed( new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "Internal interaction error occured, please report this to the developer."
}).AsEphemeral());
}
}
private static string ParseFailedCheck(SlashCheckBaseAttribute attr)
{
return attr switch
{
SlashRequireDirectMessageAttribute _ => "This command can only be used in direct messages!",
SlashRequireOwnerAttribute _ => "Only the server owner can use that command!",
SlashRequirePermissionsAttribute _ => "You don't have permission to do that!",
SlashRequireBotPermissionsAttribute _ => "The bot doesn't have the required permissions to do that!",
SlashRequireUserPermissionsAttribute _ => "You don't have permission to do that!",
SlashRequireGuildAttribute _ => "This command has to be used in a Discord server!",
_ => "Unknown Discord API error occured, please try again later."
};
}
}
}