-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandHandler.cs
More file actions
113 lines (101 loc) · 3.79 KB
/
CommandHandler.cs
File metadata and controls
113 lines (101 loc) · 3.79 KB
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
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using QuaverBot.Modules;
namespace QuaverBot;
public class CommandHandler
{
public DiscordSocketClient Client;
public InteractionService InteractionService;
private IServiceProvider services;
public CommandHandler(DiscordSocketClient client)
{
Client = client;
var config = new InteractionServiceConfig()
{
DefaultRunMode = RunMode.Async
};
InteractionService = new(Client, config);
services = new ServiceCollection()
.AddSingleton(client)
.BuildServiceProvider();
}
public async Task InstallCommandsAsync()
{
Client.Ready += () =>
{
Task.Run(RegisterCommands);
Mute.InitCheckMuted(Client);
Clean.InitClean(Client);
Logging.Init(Client);
return Task.CompletedTask;
};
InteractionService.SlashCommandExecuted += SlashCommandExecuted;
await InteractionService.AddModulesAsync(assembly: System.Reflection.Assembly.GetExecutingAssembly(), services: services);
Client.InteractionCreated += HandleInteraction;
Client.MessageReceived += Macros.OnMessageReceived;
Client.MessageReceived += (m) => Logging.OnMessageReceived(m, Client);
Client.MessageDeleted += (m, c) => Logging.OnMessageDeleted(m, c, Client);
Client.MessageUpdated += (m1, m2, c) => Logging.OnMessageUpdated(m1, m2, c, Client);
}
public async Task RegisterCommands()
{
try
{
foreach (var guild in Client.Guilds)
{
await InteractionService.RegisterCommandsToGuildAsync(guild.Id, true);
Logger.Debug("Added commands to " + guild.Id, this);
}
}
catch (Exception ex)
{
Logger.Critical(ex.Message, this, ex);
}
}
private async Task HandleInteraction(SocketInteraction interaction)
{
try
{
var context = new SocketInteractionContext(Client, interaction);
var result = await InteractionService.ExecuteCommandAsync(context, services);
}
catch (Exception ex)
{
Logger.Critical(ex.Message, this, ex);
}
}
private async Task SlashCommandExecuted(SlashCommandInfo info, IInteractionContext context, IResult result)
{
if (!result.IsSuccess)
{
switch (result.Error)
{
case InteractionCommandError.UnmetPrecondition:
await context.Interaction.RespondAsync($"Unmet Precondition: {result.ErrorReason}");
break;
case InteractionCommandError.UnknownCommand:
await context.Interaction.RespondAsync("Unknown command");
break;
case InteractionCommandError.BadArgs:
await context.Interaction.RespondAsync("Invalid number or arguments");
break;
case InteractionCommandError.Exception:
var exception = ((ExecuteResult)result).Exception;
Logger.Error(result.ErrorReason, this, exception.InnerException);
await context.Interaction.RespondAsync($"Command exception");
break;
case InteractionCommandError.Unsuccessful:
await context.Interaction.RespondAsync("Command could not be executed");
break;
default:
await context.Interaction.RespondAsync(result.Error.ToString() + ": " + result.ErrorReason);
break;
}
}
}
}