50 lines
1.9 KiB
C#
50 lines
1.9 KiB
C#
using Discord.Commands;
|
|
using Discord.WebSocket;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
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) // If not successful, reply with the error.
|
|
await context.Channel.SendMessageAsync(result.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|