mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-04-11 15:19:53 +00:00
Restructure
This commit is contained in:
24
Bot/SlashCommand/Command.cs
Normal file
24
Bot/SlashCommand/Command.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace Lunaris2.SlashCommand;
|
||||
|
||||
public static class Command
|
||||
{
|
||||
public static class Hello
|
||||
{
|
||||
public const string Name = "hello";
|
||||
public const string Description = "Say hello to the bot!";
|
||||
}
|
||||
|
||||
public static class Goodbye
|
||||
{
|
||||
public const string Name = "goodbye";
|
||||
public const string Description = "Say goodbye to the bot!";
|
||||
}
|
||||
|
||||
public static string[] GetAllCommands()
|
||||
{
|
||||
return typeof(Command)
|
||||
.GetNestedTypes()
|
||||
.Select(command => command.GetField("Name")?.GetValue(null)?.ToString())
|
||||
.ToArray()!;
|
||||
}
|
||||
}
|
||||
57
Bot/SlashCommand/SlashCommandBuilder.cs
Normal file
57
Bot/SlashCommand/SlashCommandBuilder.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using Discord.Net;
|
||||
using Discord.WebSocket;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Lunaris2.SlashCommand;
|
||||
|
||||
public class SlashCommandBuilder(string commandName, string commandDescription)
|
||||
{
|
||||
private string CommandName { get; set; } = commandName;
|
||||
private string CommandDescription { get; set; } = commandDescription;
|
||||
|
||||
public async Task CreateSlashCommand(DiscordSocketClient client)
|
||||
{
|
||||
var registeredCommands = await client.GetGlobalApplicationCommandsAsync();
|
||||
await RemoveUnusedCommands(Command.GetAllCommands(), registeredCommands);
|
||||
|
||||
if (await CommandExists(registeredCommands))
|
||||
return;
|
||||
|
||||
var globalCommand = new Discord.SlashCommandBuilder();
|
||||
globalCommand.WithName(CommandName);
|
||||
globalCommand.WithDescription(CommandDescription);
|
||||
|
||||
try
|
||||
{
|
||||
await client.CreateGlobalApplicationCommandAsync(globalCommand.Build());
|
||||
Console.WriteLine($"Command {CommandName} has been registered.");
|
||||
}
|
||||
catch(HttpException exception)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented);
|
||||
Console.WriteLine(json);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task RemoveUnusedCommands(string[] commands, IEnumerable<SocketApplicationCommand> registeredCommands)
|
||||
{
|
||||
// Remove commands from Discord(registeredCommands) that are not in the list of commands
|
||||
foreach(var command in registeredCommands)
|
||||
{
|
||||
if (commands.Contains(command.Name))
|
||||
continue;
|
||||
|
||||
await command.DeleteAsync();
|
||||
Console.WriteLine($"Command {command.Name} has been removed.");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> CommandExists(IEnumerable<SocketApplicationCommand> registeredCommands)
|
||||
{
|
||||
if (!registeredCommands.Any(command => command.Name == CommandName && command.Description == CommandDescription))
|
||||
return false;
|
||||
|
||||
Console.WriteLine($"Command {CommandName} already exists.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
18
Bot/SlashCommand/SlashCommandRegistration.cs
Normal file
18
Bot/SlashCommand/SlashCommandRegistration.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Lunaris2.SlashCommand;
|
||||
|
||||
public static class SlashCommandRegistration
|
||||
{
|
||||
public static void RegisterCommands(this DiscordSocketClient client)
|
||||
{
|
||||
RegisterCommand(client, Command.Hello.Name, Command.Hello.Description);
|
||||
RegisterCommand(client, Command.Goodbye.Name, Command.Goodbye.Description);
|
||||
}
|
||||
|
||||
private static void RegisterCommand(DiscordSocketClient client, string commandName, string commandDescription)
|
||||
{
|
||||
var command = new SlashCommandBuilder(commandName, commandDescription);
|
||||
_ = command.CreateSlashCommand(client);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user