diff --git a/Sean/CommandHandler.cs b/Sean/CommandHandler.cs new file mode 100644 index 0000000..679992a --- /dev/null +++ b/Sean/CommandHandler.cs @@ -0,0 +1,78 @@ +using Discord.Commands; +using Discord.WebSocket; +using System.Reflection; +using System.Threading.Tasks; + +namespace Sean +{ + public class CommandHandler + { + #region Fields + private readonly DiscordSocketClient _client; + private readonly CommandService _commands; + #endregion Fields + + #region Constructors + public CommandHandler(DiscordSocketClient client, CommandService commands) + { + _commands = commands; + _client = client; + } + #endregion Constructors + + #region Methods + public async Task InstallCommandsAsync() + { + // 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. 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) + { + // 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 the prefix and make sure no bots trigger commands + if (!(message.HasCharPrefix('!', ref argPos) || + message.HasMentionPrefix(_client.CurrentUser, ref argPos)) || + message.Author.IsBot) + 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, 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, + argPos: 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); + } + #endregion Methods + } +} \ No newline at end of file diff --git a/Sean/Modules/PingModule.cs b/Sean/Modules/PingModule.cs new file mode 100644 index 0000000..751ea46 --- /dev/null +++ b/Sean/Modules/PingModule.cs @@ -0,0 +1,14 @@ +using Discord.Commands; +using System.Threading.Tasks; + +namespace Sean.Modules +{ + public class PingModule : ModuleBase + { + #region Methods + [Command("ping")] + [Summary("Ping pong !")] + public Task PongAsync() => ReplyAsync("Pong !"); + #endregion Methods + } +} \ No newline at end of file diff --git a/Sean/Program.cs b/Sean/Program.cs index 30a898e..1eae022 100644 --- a/Sean/Program.cs +++ b/Sean/Program.cs @@ -1,4 +1,5 @@ using Discord; +using Discord.Commands; using Discord.WebSocket; using System; using System.Threading.Tasks; @@ -8,20 +9,29 @@ namespace Sean internal class Program { #region Fields - private DiscordSocketClient _client; + private DiscordSocketClient client; + private CommandHandler commandHandler; + private CommandService commandService; + private CommandServiceConfig commandServiceConfig; #endregion Fields #region Methods public async Task MainAsync() { - _client = new DiscordSocketClient(); + commandServiceConfig = new CommandServiceConfig + { + CaseSensitiveCommands = false, + LogLevel = LogSeverity.Debug + }; - _client.Log += Log; + commandService = new CommandService(commandServiceConfig); + client = new DiscordSocketClient(); + client.Log += Log; + commandHandler = new CommandHandler(client, commandService); + await commandHandler.InstallCommandsAsync(); - _client.MessageReceived += MessageReceived; - - await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("DiscordToken_Sean")); - await _client.StartAsync(); + await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("DiscordToken_Sean")); + await client.StartAsync(); // Block this task until the program is closed. await Task.Delay(-1); @@ -33,13 +43,6 @@ namespace Sean Console.WriteLine(msg.ToString()); return Task.CompletedTask; } - private async Task MessageReceived(SocketMessage message) - { - if (message.Content == "!ping") - { - await message.Channel.SendMessageAsync("Pong!"); - } - } #endregion Methods } } \ No newline at end of file