Add Join and Play handlers, Expand Command Builder functionality

This commit is contained in:
Myx
2024-04-14 16:06:18 +02:00
parent f7b54ca80c
commit 95ab1281a4
16 changed files with 293 additions and 20 deletions

View File

@@ -1,5 +1,12 @@
using Discord;
namespace Lunaris2.SlashCommand;
public static class Option
{
public const string Input = "input";
}
public static class Command
{
public static class Hello
@@ -14,6 +21,29 @@ public static class Command
public const string Description = "Say goodbye to the bot!";
}
public static class Join
{
public const string Name = "join";
public const string Description = "Join the voice channel!";
}
public static class Play
{
public const string Name = "play";
public const string Description = "Play a song!";
public static readonly List<SlashCommandOptionBuilder> Options = new()
{
new SlashCommandOptionBuilder
{
Name = "input",
Description = "The song you want to play",
Type = ApplicationCommandOptionType.String,
IsRequired = true
},
};
}
public static string[] GetAllCommands()
{
return typeof(Command)

View File

@@ -1,13 +1,15 @@
using Discord;
using Discord.Net;
using Discord.WebSocket;
using Newtonsoft.Json;
namespace Lunaris2.SlashCommand;
public class SlashCommandBuilder(string commandName, string commandDescription)
public class SlashCommandBuilder(string commandName, string commandDescription, List<SlashCommandOptionBuilder> commandOptions = null)
{
private string CommandName { get; set; } = commandName;
private string CommandDescription { get; set; } = commandDescription;
private List<SlashCommandOptionBuilder> CommandOptions { get; set; }
public async Task CreateSlashCommand(DiscordSocketClient client)
{
@@ -20,6 +22,8 @@ public class SlashCommandBuilder(string commandName, string commandDescription)
var globalCommand = new Discord.SlashCommandBuilder();
globalCommand.WithName(CommandName);
globalCommand.WithDescription(CommandDescription);
commandOptions.ForEach(option => globalCommand.AddOption(option));
try
{
@@ -46,12 +50,12 @@ public class SlashCommandBuilder(string commandName, string commandDescription)
}
}
private async Task<bool> CommandExists(IEnumerable<SocketApplicationCommand> registeredCommands)
private Task<bool> CommandExists(IEnumerable<SocketApplicationCommand> registeredCommands)
{
if (!registeredCommands.Any(command => command.Name == CommandName && command.Description == CommandDescription))
return false;
return Task.FromResult(false);
Console.WriteLine($"Command {CommandName} already exists.");
return true;
return Task.FromResult(true);
}
}

View File

@@ -1,3 +1,4 @@
using Discord;
using Discord.WebSocket;
namespace Lunaris2.SlashCommand;
@@ -8,11 +9,13 @@ public static class SlashCommandRegistration
{
RegisterCommand(client, Command.Hello.Name, Command.Hello.Description);
RegisterCommand(client, Command.Goodbye.Name, Command.Goodbye.Description);
RegisterCommand(client, Command.Join.Name, Command.Join.Description);
RegisterCommand(client, Command.Play.Name, Command.Play.Description, Command.Play.Options);
}
private static void RegisterCommand(DiscordSocketClient client, string commandName, string commandDescription)
private static void RegisterCommand(DiscordSocketClient client, string commandName, string commandDescription, List<SlashCommandOptionBuilder> commandOptions = null)
{
var command = new SlashCommandBuilder(commandName, commandDescription);
var command = new SlashCommandBuilder(commandName, commandDescription, commandOptions);
_ = command.CreateSlashCommand(client);
}
}