The slash commands can now use interactions

This commit is contained in:
2024-04-17 17:45:35 +03:00
parent 123e8e90a1
commit f32920c564
11 changed files with 131 additions and 20 deletions

View File

@@ -1,10 +1,12 @@
using System.Linq;
using PluginManager;
using PluginManager.Loaders;
namespace DiscordBot.Bot.Actions.Extra;
internal static class SettingsConfigExtra
{
internal static void SetSettings(string key, params string[] value)
{
if (key is null) return;

View File

@@ -14,6 +14,8 @@ public class Help: DBSlashCommand
public string Description => "This command allows you to check all loaded commands";
public bool canUseDM => true;
public bool HasInteraction => false;
public List<SlashCommandOptionBuilder> Options =>
new()
{

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExtendedDiscordUIControls.Controls.SubItems;
namespace ExtendedDiscordUIControls.Controls
{
public class Dropdown
{
public List<DropDownItem> Items { get; private set; }
public Dropdown(List<DropDownItem> items) {
Items = items;
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExtendedDiscordUIControls.Controls.SubItems
{
public class DropDownItem
{
public string Title { get; private set; }
public string Description { get; private set; }
public DropDownItem(string title, string description)
{
Title = title;
Description = description;
}
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PluginManager\PluginManager.csproj" />
</ItemGroup>
</Project>

View File

@@ -51,7 +51,6 @@ internal class CommandHandler
if (plugin is null)
throw new Exception("Failed to run command !");
if (arg.Channel is SocketDMChannel)
plugin.ExecuteDM(arg);
else plugin.ExecuteServer(arg);

View File

@@ -30,6 +30,9 @@ public class Config
public static Logger Logger;
public static SettingsDictionary<string, string> AppSettings;
internal static string PluginDatabase => AppSettings["PluginDatabase"];
internal static string ServerID => AppSettings["ServerID"];
public static InternalActionManager InternalActionManager;
public static PluginsManager PluginsManager;

View File

@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
@@ -8,14 +10,15 @@ public interface DBSlashCommand
{
string Name { get; }
string Description { get; }
bool canUseDM { get; }
bool HasInteraction { get; }
List<SlashCommandOptionBuilder> Options { get; }
void ExecuteServer(SocketSlashCommand context)
{
}
{ }
void ExecuteDM(SocketSlashCommand context) { }
Task ExecuteInteraction(SocketInteraction interaction) => Task.CompletedTask;
}

View File

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

View File

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

View File

@@ -62,28 +62,28 @@ public class PluginsManager
public async Task RemovePluginFromDatabase(string pluginName)
{
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Config.AppSettings["PluginDatabase"]));
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Config.PluginDatabase));
installedPlugins.RemoveAll(p => p.PluginName == pluginName);
await JsonManager.SaveToJsonFile( Config.AppSettings["PluginDatabase"],installedPlugins);
await JsonManager.SaveToJsonFile( Config.PluginDatabase,installedPlugins);
}
public async Task AppendPluginToDatabase(PluginInfo pluginData)
{
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Config.AppSettings["PluginDatabase"]));
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Config.PluginDatabase));
installedPlugins.Add(pluginData);
await JsonManager.SaveToJsonFile( Config.AppSettings["PluginDatabase"],installedPlugins);
await JsonManager.SaveToJsonFile( Config.PluginDatabase, installedPlugins);
}
public async Task<List<PluginInfo>> GetInstalledPlugins()
{
return await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Config.AppSettings["PluginDatabase"]));
return await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Config.PluginDatabase));
}
public async Task<bool> IsPluginInstalled(string pluginName)
{
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Config.AppSettings["PluginDatabase"]));
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Config.PluginDatabase));
return installedPlugins.Any(plugin => plugin.PluginName == pluginName);
}