-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandHandler.cs
64 lines (53 loc) · 2.46 KB
/
CommandHandler.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
using Discord.Commands;
using Discord.WebSocket;
using System.Reflection;
using System.Threading.Tasks;
namespace bot.Handlers
{
public class CommandHandler
{
private readonly DiscordSocketClient client;
private readonly CommandService commandService;
// Retrieve client and CommandService instance via constructor
public CommandHandler(DiscordSocketClient client, CommandService commandService)
{
this.client = client;
this.commandService = commandService;
}
public async Task SetupAsync()
{
// Hook the MessageReceived event into our command handler
client.MessageReceived += HandleCommandAsync;
// Here we discover all of the command modules in the entry assembly and load them
await commandService.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: null);
}
private async Task HandleCommandAsync(SocketMessage messageParam)
{
// Don't process the command if it was a system message
var message = messageParam as SocketUserMessage;
if (message == null) return;
// Create a number to track where the prefix ends and the command begins
int argPos = 0;
// Reacts to events
if (message.Author.IsBot && message.Channel.Id == Global.EventsId)
{
await AddReaction.AddReactionAsync(message);
}
// Determine if the message is a command based on the prefix and make sure no bots trigger commands
if (!message.HasCharPrefix('!', ref argPos) ||
message.Author.IsBot || message.Author.Id != Global.MyId)
return;
// Create a WebSocket-based command context based on the message
var context = new SocketCommandContext(client, message);
// Execute the command with the command context we just created
var result = await commandService.ExecuteAsync(context: context, argPos: argPos, services: null);
if (!result.IsSuccess)
{
await context.Channel.DeleteMessageAsync(message);
var msg = await context.Channel.SendMessageAsync(result.ErrorReason);
await Task.Delay(5000);
await context.Channel.DeleteMessageAsync(msg);
}
}
}
}