|
@@ -2,38 +2,62 @@ public class CommandHandler |
|
|
{ |
|
|
{ |
|
|
private readonly DiscordSocketClient _client; |
|
|
private readonly DiscordSocketClient _client; |
|
|
private readonly CommandService _commands; |
|
|
private readonly CommandService _commands; |
|
|
private readonly IServiceProvider _services; |
|
|
|
|
|
|
|
|
|
|
|
public CommandHandler(IServiceProvider services) |
|
|
|
|
|
|
|
|
public CommandHandler(DiscordSocketClient client, CommandService commands) |
|
|
{ |
|
|
{ |
|
|
_services = services; |
|
|
|
|
|
_commands = services.GetRequiredService<CommandService>(); |
|
|
|
|
|
_client = services.GetRequiredService<DiscordSocketClient>(); |
|
|
|
|
|
|
|
|
_commands = commands; |
|
|
|
|
|
_client = client; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task InstallCommandsAsync() |
|
|
public async Task InstallCommandsAsync() |
|
|
{ |
|
|
{ |
|
|
// Hook the MessageReceived Event into our Command Handler |
|
|
|
|
|
|
|
|
// Hook the MessageReceived event into our command handler |
|
|
_client.MessageReceived += HandleCommandAsync; |
|
|
_client.MessageReceived += HandleCommandAsync; |
|
|
// Discover all of the commands in this assembly and load them. |
|
|
|
|
|
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Here we discover all of the command modules in the entry |
|
|
|
|
|
// assembly and load them. Starting from Discord.NET 2.0, a |
|
|
|
|
|
// service provider is required to be passed into the |
|
|
|
|
|
// module registration method to inject the |
|
|
|
|
|
// required dependencies. |
|
|
|
|
|
// |
|
|
|
|
|
// If you do not use Dependency Injection, pass null. |
|
|
|
|
|
// See Dependency Injection guide for more information. |
|
|
|
|
|
await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), |
|
|
|
|
|
services: null); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private async Task HandleCommandAsync(SocketMessage messageParam) |
|
|
private async Task HandleCommandAsync(SocketMessage messageParam) |
|
|
{ |
|
|
{ |
|
|
// Don't process the command if it was a System Message |
|
|
|
|
|
|
|
|
// Don't process the command if it was a system message |
|
|
var message = messageParam as SocketUserMessage; |
|
|
var message = messageParam as SocketUserMessage; |
|
|
if (message == null) return; |
|
|
if (message == null) return; |
|
|
|
|
|
|
|
|
// Create a number to track where the prefix ends and the command begins |
|
|
// Create a number to track where the prefix ends and the command begins |
|
|
int argPos = 0; |
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Determine if the message is a command based on the prefix |
|
|
|
|
|
if (!(message.HasCharPrefix('!', ref argPos) || |
|
|
|
|
|
message.HasMentionPrefix(_client.CurrentUser, ref argPos))) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
// Create a WebSocket-based command context based on the message |
|
|
var context = new SocketCommandContext(_client, message); |
|
|
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); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Execute the command with the command context we just |
|
|
|
|
|
// created, along with the service provider for precondition checks. |
|
|
|
|
|
|
|
|
|
|
|
// Keep in mind that result does not indicate a return value |
|
|
|
|
|
// rather an object stating if the command executed successfully. |
|
|
|
|
|
var result = await _commands.ExecuteAsync( |
|
|
|
|
|
context: context, |
|
|
|
|
|
argPost: argPos, |
|
|
|
|
|
services: null); |
|
|
|
|
|
|
|
|
|
|
|
// Optionally, we may inform the user if the command fails |
|
|
|
|
|
// to be executed; however, this may not always be desired, |
|
|
|
|
|
// as it may clog up the request queue should a user spam a |
|
|
|
|
|
// command. |
|
|
|
|
|
// if (!result.IsSuccess) |
|
|
|
|
|
// await context.Channel.SendMessageAsync(result.ErrorReason); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |