2017-12-05 05:59:37 +00:00
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-12-05 17:10:21 +00:00
|
|
|
|
using dotbot.Core;
|
|
|
|
|
using System.Linq;
|
2017-12-05 05:59:37 +00:00
|
|
|
|
|
|
|
|
|
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
|
2017-12-06 05:28:58 +00:00
|
|
|
|
if (result.IsSuccess) return;
|
2017-12-05 05:59:37 +00:00
|
|
|
|
|
2017-12-06 05:28:58 +00:00
|
|
|
|
if (msg.HasStringPrefix(_config["prefix"], ref argPos))
|
2017-12-05 17:10:21 +00:00
|
|
|
|
{
|
2017-12-06 05:28:58 +00:00
|
|
|
|
using (var db = new DotbotDbContext())
|
2017-12-05 17:10:21 +00:00
|
|
|
|
{
|
2017-12-06 05:28:58 +00:00
|
|
|
|
var key = msg.Content.Substring(_config["prefix"].Length);
|
|
|
|
|
if (db.Defs.Any(d => d.Id == key))
|
2017-12-05 17:15:02 +00:00
|
|
|
|
{
|
2017-12-06 05:28:58 +00:00
|
|
|
|
await context.Channel.SendMessageAsync($"**{key}**: {db.Defs.Find(key).Def}");
|
|
|
|
|
return;
|
2017-12-05 17:15:02 +00:00
|
|
|
|
}
|
2017-12-05 17:10:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-06 05:28:58 +00:00
|
|
|
|
if (!result.ToString().Contains("UnknownCommand"))
|
|
|
|
|
await context.Channel.SendMessageAsync(result.ToString());
|
|
|
|
|
|
2017-12-05 17:10:21 +00:00
|
|
|
|
}
|
2017-12-05 17:15:02 +00:00
|
|
|
|
|
2017-12-05 05:59:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|