Reworked the Config system
This commit is contained in:
@@ -3,10 +3,12 @@ using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Discord;
|
||||
using Discord.Interactions;
|
||||
using Discord.WebSocket;
|
||||
|
||||
using DiscordBotCore.Interfaces;
|
||||
using DiscordBotCore.Others;
|
||||
using ContextType = Discord.Commands.ContextType;
|
||||
|
||||
namespace DiscordBotCore.Loaders;
|
||||
|
||||
@@ -31,60 +33,75 @@ internal static class PluginLoaderExtensions
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static async Task ResetSlashCommands(this PluginLoader pluginLoader)
|
||||
|
||||
internal static Task ResetSlashCommands(this PluginLoader pluginLoader)
|
||||
{
|
||||
await pluginLoader._Client.Rest.DeleteAllGlobalCommandsAsync();
|
||||
|
||||
if(pluginLoader._Client.Guilds.Count == 0) return;
|
||||
if (!ulong.TryParse(Application.CurrentApplication.ServerID, out _))
|
||||
{
|
||||
Application.CurrentApplication.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(Application.CurrentApplication.ServerID));
|
||||
if(guild is null)
|
||||
{
|
||||
Application.CurrentApplication.Logger.Log("Failed to get guild with ID " + Application.CurrentApplication.ServerID, typeof(PluginLoader), LogType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
await guild.DeleteApplicationCommandsAsync();
|
||||
|
||||
Application.CurrentApplication.Logger.Log($"Cleared all slash commands from guild {guild.Id}", typeof(PluginLoader));
|
||||
throw new NotImplementedException("This method is not implemented yet.");
|
||||
|
||||
// await pluginLoader._Client.Rest.DeleteAllGlobalCommandsAsync();
|
||||
//
|
||||
// if(pluginLoader._Client.Guilds.Count == 0) return;
|
||||
// if (!ulong.TryParse(Application.CurrentApplication.ServerID, out _))
|
||||
// {
|
||||
// Application.CurrentApplication.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(Application.CurrentApplication.ServerID));
|
||||
// if(guild is null)
|
||||
// {
|
||||
// Application.CurrentApplication.Logger.Log("Failed to get guild with ID " + Application.CurrentApplication.ServerID, typeof(PluginLoader), LogType.Error);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// await guild.DeleteApplicationCommandsAsync();
|
||||
//
|
||||
// Application.CurrentApplication.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));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pluginLoader._Client.Guilds.Count == 0) 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;
|
||||
|
||||
if (uint.TryParse(Application.CurrentApplication.ServerID, out uint result))
|
||||
{
|
||||
SocketGuild? guild = pluginLoader._Client.GetGuild(result);
|
||||
if (guild is null)
|
||||
{
|
||||
Application.CurrentApplication.Logger.Log("Failed to get guild with ID " + Application.CurrentApplication.ServerID, typeof(PluginLoader), LogType.Error);
|
||||
return false;
|
||||
}
|
||||
if (dbSlashCommand.canUseDM)
|
||||
builder.WithContextTypes(InteractionContextType.BotDm, InteractionContextType.Guild);
|
||||
else
|
||||
builder.WithContextTypes(InteractionContextType.Guild);
|
||||
|
||||
await guild.CreateApplicationCommandAsync(builder.Build());
|
||||
}else await pluginLoader._Client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
// if (uint.TryParse(Application.CurrentApplication.ServerID, out uint result))
|
||||
// {
|
||||
// SocketGuild? guild = pluginLoader._Client.GetGuild(result);
|
||||
// if (guild is null)
|
||||
// {
|
||||
// Application.CurrentApplication.Logger.Log("Failed to get guild with ID " + Application.CurrentApplication.ServerID, typeof(PluginLoader), LogType.Error);
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// await guild.CreateApplicationCommandAsync(builder.Build());
|
||||
// }
|
||||
// else
|
||||
|
||||
foreach(ulong guildId in Application.CurrentApplication.ServerIDs)
|
||||
{
|
||||
await pluginLoader.EnableSlashCommandPerGuild(guildId, builder);
|
||||
}
|
||||
|
||||
await pluginLoader._Client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -94,4 +111,19 @@ internal static class PluginLoaderExtensions
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<bool> EnableSlashCommandPerGuild(this PluginLoader pluginLoader, ulong guildId, SlashCommandBuilder builder)
|
||||
{
|
||||
SocketGuild? guild = pluginLoader._Client.GetGuild(guildId);
|
||||
if (guild is null)
|
||||
{
|
||||
Application.CurrentApplication.Logger.Log("Failed to get guild with ID " + guildId, typeof(PluginLoader), LogType.Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
await guild.CreateApplicationCommandAsync(builder.Build());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user