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;
@@ -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>

View File

@@ -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<SlashCommandOptionBuilder> Options { get; }
void ExecuteServer(SocketSlashCommand context)
{
}
void ExecuteDM(SocketSlashCommand context) { }
}
}

View File

@@ -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
/// </summary>
public static List<DBEvent>? Events { get; set; }
/// <summary>
/// A list of <see cref="DBSlashCommand"/> commands
/// </summary>
public static List<DBSlashCommand>? SlashCommands { get; set; }
/// <summary>
/// The main mathod that is called to load all events
/// </summary>
@@ -103,13 +109,14 @@ public class PluginLoader
Commands = new List<DBCommand>();
Events = new List<DBEvent>();
SlashCommands = new List<DBSlashCommand>();
Functions.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username);
Console.WriteLine("Loading plugins");
var commandsLoader = new Loader<DBCommand>(pluginCMDFolder, pluginCMDExtension);
var eventsLoader = new Loader<DBEvent>(pluginEVEFolder, pluginEVEExtension);
var slashLoader = new Loader<DBSlashCommand>("./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)

View File

@@ -16,7 +16,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.8.1" />
<PackageReference Include="Discord.Net" Version="3.7.2" />
<PackageReference Include="Terminal.Gui" Version="1.8.2" />
</ItemGroup>

View File

@@ -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