using Discord.Commands; using Discord.WebSocket; using Microsoft.Extensions.Configuration; using System; using System.Threading.Tasks; using dotbot.Core; using System.Linq; namespace dotbot.Services { public class CommandHandlerService { private readonly DiscordSocketClient _discord; private readonly CommandService _commands; private readonly IConfigurationRoot _config; private readonly IServiceProvider _provider; // DiscordSocketClient, CommandService, IConfigurationRoot, and IServiceProvider are injected automatically from the IServiceProvider public CommandHandlerService( DiscordSocketClient discord, CommandService commands, IConfigurationRoot config, IServiceProvider provider) { _discord = discord; _commands = commands; _config = config; _provider = provider; _discord.MessageReceived += OnMessageReceivedAsync; } private async Task OnMessageReceivedAsync(SocketMessage s) { var msg = s as SocketUserMessage; // Ensure the message is from a user/bot if (msg == null) return; if (msg.Author.Id == _discord.CurrentUser.Id) return; // Ignore self when checking commands var context = new SocketCommandContext(_discord, msg); // Create the command context int argPos = 0; // Check if the message has a valid command prefix if (msg.HasStringPrefix(_config["prefix"], ref argPos) || msg.HasMentionPrefix(_discord.CurrentUser, ref argPos)) { var result = await _commands.ExecuteAsync(context, argPos, _provider); // Execute the command if (result.IsSuccess) return; if (msg.HasStringPrefix(_config["prefix"], ref argPos)) { using (var db = new DotbotDbContext()) { var key = msg.Content.Substring(_config["prefix"].Length); if (db.Defs.Any(d => d.Id == key)) { await context.Channel.SendMessageAsync($"**{key}**: {db.Defs.Find(key).Def}"); return; } } } if (!result.ToString().Contains("UnknownCommand")) await context.Channel.SendMessageAsync(result.ToString()); } } } }