This commit is contained in:
2022-10-22 16:02:05 +03:00
parent a4975a4578
commit cbaf552e7a
6 changed files with 102 additions and 9 deletions

View File

@@ -2,8 +2,10 @@
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Loaders;
using PluginManager.Others;
using PluginManager.Others.Permissions;
@@ -12,9 +14,9 @@ namespace DiscordBot.Discord.Core;
internal class CommandHandler
{
private readonly string botPrefix;
private readonly string botPrefix;
private readonly DiscordSocketClient client;
private readonly CommandService commandService;
private readonly CommandService commandService;
/// <summary>
/// Command handler constructor
@@ -24,9 +26,9 @@ internal class CommandHandler
/// <param name="botPrefix">The prefix to watch for</param>
public CommandHandler(DiscordSocketClient client, CommandService commandService, string botPrefix)
{
this.client = client;
this.client = client;
this.commandService = commandService;
this.botPrefix = botPrefix;
this.botPrefix = botPrefix;
}
/// <summary>
@@ -36,9 +38,34 @@ internal class CommandHandler
public async Task InstallCommandsAsync()
{
client.MessageReceived += MessageHandler;
client.SlashCommandExecuted += Client_SlashCommandExecuted;
await commandService.AddModulesAsync(Assembly.GetEntryAssembly(), null);
}
private async Task Client_SlashCommandExecuted(SocketSlashCommand arg)
{
try
{
var plugin = PluginLoader.SlashCommands!
.Where(p => p.Name == arg.Data.Name)
.FirstOrDefault();
if (plugin is null) throw new Exception("Failed to run command. !");
if (arg.Channel is SocketDMChannel)
plugin.ExecuteDM(arg);
else plugin.ExecuteServer(arg);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
ex.WriteErrFile();
}
}
/// <summary>
/// The message handler for the bot
/// </summary>

View File

@@ -38,7 +38,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.8.1" />
<PackageReference Include="Discord.Net" Version="3.7.2" />
</ItemGroup>
<ItemGroup>