kasbot/Kasbot.APP/Services/CommandHandlingService.cs

65 lines
2.0 KiB
C#

using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Kasbot.Extensions;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;
using System.Threading.Tasks;
namespace Kasbot.Services
{
public class CommandHandlingService
{
private readonly CommandService _commands;
private readonly DiscordShardedClient _discord;
private readonly IServiceProvider _services;
private readonly string CommandPrefix = "!";
public CommandHandlingService(IServiceProvider services)
{
_commands = services.GetRequiredService<CommandService>();
_discord = services.GetRequiredService<DiscordShardedClient>();
_services = services;
_commands.CommandExecuted += CommandExecutedAsync;
_discord.MessageReceived += MessageReceivedAsync;
CommandPrefix = Environment.GetEnvironmentVariable("COMMAND_PREFIX") ?? "!";
}
public async Task InitializeAsync()
{
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
public async Task MessageReceivedAsync(SocketMessage rawMessage)
{
if (!(rawMessage is SocketUserMessage message))
return;
if (message.Source != MessageSource.User)
return;
var argPos = 0;
//Check if the message sent has the specified prefix
if (!message.HasStringPrefix(CommandPrefix, ref argPos)) return;
var context = new ShardedCommandContext(_discord, message);
await _commands.ExecuteAsync(context, argPos, _services);
}
public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
{
if (!command.IsSpecified)
return;
if (result.IsSuccess)
return;
await context.Channel.SendTemporaryMessageAsync($"Error: {result}");
}
}
}