The slash commands can now use interactions
This commit is contained in:
@@ -36,6 +36,8 @@ public class PluginLoader
|
||||
|
||||
var loader = new Loader(Config.AppSettings["PluginFolder"], "dll");
|
||||
|
||||
//await this.ResetSlashCommands();
|
||||
|
||||
loader.OnFileLoadedException += FileLoadedException;
|
||||
loader.OnPluginLoaded += OnPluginLoaded;
|
||||
|
||||
@@ -47,7 +49,7 @@ public class PluginLoader
|
||||
Config.Logger.Log(result.ErrorMessage, typeof(PluginLoader), LogType.ERROR);
|
||||
}
|
||||
|
||||
private void OnPluginLoaded(PluginLoadResultData result)
|
||||
private async void OnPluginLoaded(PluginLoadResultData result)
|
||||
{
|
||||
switch (result.PluginType)
|
||||
{
|
||||
@@ -64,11 +66,15 @@ public class PluginLoader
|
||||
|
||||
break;
|
||||
case PluginType.SLASH_COMMAND:
|
||||
if (this.TryStartSlashCommand((DBSlashCommand)result.Plugin))
|
||||
if (await this.TryStartSlashCommand((DBSlashCommand)result.Plugin))
|
||||
{
|
||||
if(((DBSlashCommand)result.Plugin).HasInteraction)
|
||||
_Client.InteractionCreated += ((DBSlashCommand)result.Plugin).ExecuteInteraction;
|
||||
SlashCommands.Add((DBSlashCommand)result.Plugin);
|
||||
OnSlashCommandLoaded?.Invoke(result);
|
||||
}
|
||||
}
|
||||
else
|
||||
Config.Logger.Log($"Failed to start slash command {result.PluginName}", typeof(PluginLoader), LogType.ERROR);
|
||||
break;
|
||||
case PluginType.UNKNOWN:
|
||||
default:
|
||||
@@ -76,6 +82,4 @@ public class PluginLoader
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Others;
|
||||
|
||||
@@ -27,22 +32,60 @@ internal static class PluginLoaderExtensions
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool TryStartSlashCommand(this PluginLoader pluginLoader, DBSlashCommand? dbSlashCommand)
|
||||
internal static async Task ResetSlashCommands(this PluginLoader pluginLoader)
|
||||
{
|
||||
await pluginLoader._Client.Rest.DeleteAllGlobalCommandsAsync();
|
||||
|
||||
if(pluginLoader._Client.Guilds.Count == 0) return;
|
||||
if (!ulong.TryParse(Config.ServerID, out _))
|
||||
{
|
||||
Config.Logger.Log("Invalid ServerID in config file. Can not reset specific guild commands", typeof(PluginLoader), LogType.ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
SocketGuild? guild = pluginLoader._Client.GetGuild(ulong.Parse(Config.ServerID));
|
||||
if(guild is null)
|
||||
{
|
||||
Config.Logger.Log("Failed to get guild with ID " + Config.ServerID, typeof(PluginLoader), LogType.ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
await guild.DeleteApplicationCommandsAsync();
|
||||
|
||||
Config.Logger.Log($"Cleared all slash commands from guild {guild.Id}", typeof(PluginLoader));
|
||||
}
|
||||
|
||||
internal static async Task<bool> TryStartSlashCommand(this PluginLoader pluginLoader, DBSlashCommand? dbSlashCommand)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dbSlashCommand is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(dbSlashCommand));
|
||||
//throw new ArgumentNullException(nameof(dbSlashCommand));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pluginLoader._Client.Guilds.Count == 0) return false;
|
||||
|
||||
|
||||
var builder = new SlashCommandBuilder();
|
||||
builder.WithName(dbSlashCommand.Name);
|
||||
builder.WithDescription(dbSlashCommand.Description);
|
||||
builder.WithDMPermission(dbSlashCommand.canUseDM);
|
||||
builder.Options = dbSlashCommand.Options;
|
||||
|
||||
pluginLoader._Client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
if (uint.TryParse(Config.ServerID, out uint result))
|
||||
{
|
||||
SocketGuild? guild = pluginLoader._Client.GetGuild(result);
|
||||
if (guild is null)
|
||||
{
|
||||
Config.Logger.Log("Failed to get guild with ID " + Config.ServerID, typeof(PluginLoader), LogType.ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
await guild.CreateApplicationCommandAsync(builder.Build());
|
||||
}else await pluginLoader._Client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
Reference in New Issue
Block a user