diff --git a/DiscordBot/Discord/Core/CommandHandler.cs b/DiscordBot/Discord/Core/CommandHandler.cs index 99e0e08..100dfe4 100644 --- a/DiscordBot/Discord/Core/CommandHandler.cs +++ b/DiscordBot/Discord/Core/CommandHandler.cs @@ -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; /// /// Command handler constructor @@ -24,9 +26,9 @@ internal class CommandHandler /// The prefix to watch for public CommandHandler(DiscordSocketClient client, CommandService commandService, string botPrefix) { - this.client = client; + this.client = client; this.commandService = commandService; - this.botPrefix = botPrefix; + this.botPrefix = botPrefix; } /// @@ -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(); + } + + } + /// /// The message handler for the bot /// diff --git a/DiscordBot/DiscordBot.csproj b/DiscordBot/DiscordBot.csproj index 73f747c..f4e50a3 100644 --- a/DiscordBot/DiscordBot.csproj +++ b/DiscordBot/DiscordBot.csproj @@ -38,7 +38,7 @@ - + diff --git a/PluginManager/Interfaces/DBSlashCommand.cs b/PluginManager/Interfaces/DBSlashCommand.cs new file mode 100644 index 0000000..a1c3ab0 --- /dev/null +++ b/PluginManager/Interfaces/DBSlashCommand.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; + +using Discord; +using Discord.WebSocket; + +namespace PluginManager.Interfaces +{ + public interface DBSlashCommand + { + string Name { get; } + string Description { get; } + + bool canUseDM { get; } + + List Options { get; } + + void ExecuteServer(SocketSlashCommand context) + { + + } + + void ExecuteDM(SocketSlashCommand context) { } + + } +} diff --git a/PluginManager/Loaders/PluginLoader.cs b/PluginManager/Loaders/PluginLoader.cs index 6958e4f..72e68a0 100644 --- a/PluginManager/Loaders/PluginLoader.cs +++ b/PluginManager/Loaders/PluginLoader.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Threading.Tasks; +using Discord; using Discord.WebSocket; using PluginManager.Interfaces; @@ -55,6 +56,11 @@ public class PluginLoader /// public static List? Events { get; set; } + /// + /// A list of commands + /// + public static List? SlashCommands { get; set; } + /// /// The main mathod that is called to load all events /// @@ -103,13 +109,14 @@ public class PluginLoader Commands = new List(); Events = new List(); + SlashCommands = new List(); Functions.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username); Console.WriteLine("Loading plugins"); var commandsLoader = new Loader(pluginCMDFolder, pluginCMDExtension); var eventsLoader = new Loader(pluginEVEFolder, pluginEVEExtension); - + var slashLoader = new Loader("./Data/Plugins/SlashCommands/", "dll"); commandsLoader.FileLoaded += OnCommandFileLoaded; commandsLoader.PluginLoaded += OnCommandLoaded; @@ -117,11 +124,39 @@ public class PluginLoader eventsLoader.FileLoaded += EventFileLoaded; eventsLoader.PluginLoaded += OnEventLoaded; + slashLoader.FileLoaded += SlashLoader_FileLoaded; + slashLoader.PluginLoaded += SlashLoader_PluginLoaded; + Commands = commandsLoader.Load(); Events = eventsLoader.Load(); + SlashCommands = slashLoader.Load(); } + private async void SlashLoader_PluginLoaded(LoaderArgs args) + { + if (args.IsLoaded) + { + var slash = (DBSlashCommand)args.Plugin; + SlashCommandBuilder builder = new SlashCommandBuilder(); + builder.WithName(slash.Name); + builder.WithDescription(slash.Description); + builder.WithDMPermission(slash.canUseDM); + builder.Options = slash.Options; + Console.WriteLine("Loaded " + slash.Name); + await _client.CreateGlobalApplicationCommandAsync(builder.Build()); + + + } + else Console.WriteLine("Failed to load " + args.PluginName + "\nException: " + args.Exception.Message); + } + + private void SlashLoader_FileLoaded(LoaderArgs args) + { + if (!args.IsLoaded) + Functions.WriteLogFile($"[SLASH] Event from file [{args.PluginName}] has been successfully created !"); + } + private void EventFileLoaded(LoaderArgs e) { if (!e.IsLoaded) diff --git a/PluginManager/PluginManager.csproj b/PluginManager/PluginManager.csproj index 6430973..1a1adb4 100644 --- a/PluginManager/PluginManager.csproj +++ b/PluginManager/PluginManager.csproj @@ -16,7 +16,7 @@ - + diff --git a/SethDiscordBot.sln b/SethDiscordBot.sln index 93c8ecc..5f59372 100644 --- a/SethDiscordBot.sln +++ b/SethDiscordBot.sln @@ -7,9 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBot", "DiscordBot\Di EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluginManager", "PluginManager\PluginManager.csproj", "{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MusicLibrary", "..\DiscordBotItems\Plugins\MusicLibrary\MusicLibrary.csproj", "{878DFE01-4596-4EBC-9651-0679598CE794}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MusicLibrary", "..\DiscordBotItems\Plugins\MusicLibrary\MusicLibrary.csproj", "{878DFE01-4596-4EBC-9651-0679598CE794}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MusicLibraryEvent", "..\DiscordBotItems\Plugins\MusicLibraryEvent\MusicLibraryEvent.csproj", "{3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MusicLibraryEvent", "..\DiscordBotItems\Plugins\MusicLibraryEvent\MusicLibraryEvent.csproj", "{3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlashCommands", "..\DiscordBotItems\Plugins\SlashCommands\SlashCommands.csproj", "{C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +35,10 @@ Global {3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}.Debug|Any CPU.Build.0 = Debug|Any CPU {3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}.Release|Any CPU.ActiveCfg = Release|Any CPU {3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}.Release|Any CPU.Build.0 = Release|Any CPU + {C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE