changing folder structure
This commit is contained in:
64
Kasbot.APP/Services/CommandHandlingService.cs
Normal file
64
Kasbot.APP/Services/CommandHandlingService.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user