-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandHandler.cs
49 lines (45 loc) · 1.88 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
using System.Threading.Tasks;
using System.Reflection;
using Discord;
using Discord.WebSocket;
using Discord.Commands;
namespace GarlicBot
{
public class CommandHandler
{
public CommandHandler(DiscordSocketClient client)
{
_client = client;
_service = new CommandService();
_service.AddModulesAsync(Assembly.GetEntryAssembly());
_client.MessageReceived += HandleCommandAsync;
}
private async Task HandleCommandAsync(SocketMessage s)
{
SocketUserMessage msg = s as SocketUserMessage;
if(msg != null)
{
SocketCommandContext context = new SocketCommandContext(_client, msg);
await Utilities.LogAsync($"{context.User.Username}: {context.Message.Content}", LogSeverity.Verbose);
int argPos = 0;
if (msg.HasStringPrefix(Config.bot.commandPrefix, ref argPos)
|| msg.HasMentionPrefix(_client.CurrentUser, ref argPos))
{
var result = await _service.ExecuteAsync(context, argPos);
if(!result.IsSuccess)
{
await Utilities.LogAsync(result.ErrorReason, LogSeverity.Warning);
var embed = new EmbedBuilder();
embed.WithTitle(await Utilities.GetAlert("commandErrorTitle"));
embed.WithDescription(result.ErrorReason);
embed.WithColor(await Utilities.ParseColor(Config.bot.embedColor));
embed.WithAuthor(Config.bot.botName, Config.bot.botIconURL);
await context.Channel.SendMessageAsync("", false, embed.Build());
}
}
}
}
private DiscordSocketClient _client;
private CommandService _service;
}
}