|
- public class CommandHandle
- {
- private readonly DiscordSocketClient _client;
- private readonly CommandService _commands;
-
- public CommandHandle(DiscordSocketClient client)
- {
- _client = client;
- _commands = new CommandService();
- }
-
- public async Task InstallCommandsAsync()
- {
- // Hook the MessageReceived Event into our Command Handler
- _client.MessageReceived += HandleCommandAsync;
- // Discover all of the commands in this assembly and load them.
- await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
- }
-
- 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;
- // Determine if the message is a command, based on if it starts with '!' or a mention prefix
- if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return;
- // Create a Command Context
- var context = new SocketCommandContext(_client, message);
- // Execute the command. (result does not indicate a return value,
- // rather an object stating if the command executed successfully)
- var result = await _commands.ExecuteAsync(context, argPos, _services);
- if (!result.IsSuccess)
- await context.Channel.SendMessageAsync(result.ErrorReason);
- }
- }
|