From f33866efc1f90cb472c68a0234f5fe0076254d15 Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 18 Oct 2023 13:48:15 +0200 Subject: [PATCH] setting up slash commands --- Tools/BiblioTech/BiblioTech.csproj | 1 + Tools/BiblioTech/CommandHandler.cs | 7 ++- Tools/BiblioTech/DeploymentFilesMonitor.cs | 47 +++++++++++++++++ Tools/BiblioTech/HelloWorldCommand.cs | 61 ++++++++++++++++++++++ Tools/BiblioTech/Program.cs | 16 ++++-- Tools/BiblioTech/docker/Dockerfile | 3 +- 6 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 Tools/BiblioTech/DeploymentFilesMonitor.cs create mode 100644 Tools/BiblioTech/HelloWorldCommand.cs diff --git a/Tools/BiblioTech/BiblioTech.csproj b/Tools/BiblioTech/BiblioTech.csproj index 0fc6193..9c32ad4 100644 --- a/Tools/BiblioTech/BiblioTech.csproj +++ b/Tools/BiblioTech/BiblioTech.csproj @@ -10,6 +10,7 @@ + diff --git a/Tools/BiblioTech/CommandHandler.cs b/Tools/BiblioTech/CommandHandler.cs index ef8330a..42290a5 100644 --- a/Tools/BiblioTech/CommandHandler.cs +++ b/Tools/BiblioTech/CommandHandler.cs @@ -43,10 +43,9 @@ namespace BiblioTech int argPos = 0; // Determine if the message is a command based on the prefix and make sure no bots trigger commands - if (!(message.HasCharPrefix('!', ref argPos) || - message.HasMentionPrefix(client.CurrentUser, ref argPos)) || - message.Author.IsBot) - return; + if (message.Author.IsBot) return; + if (!message.HasMentionPrefix(client.CurrentUser, ref argPos) && + !message.Content.StartsWith("!")) return; // Create a WebSocket-based command context based on the message var context = new SocketCommandContext(client, message); diff --git a/Tools/BiblioTech/DeploymentFilesMonitor.cs b/Tools/BiblioTech/DeploymentFilesMonitor.cs new file mode 100644 index 0000000..0d8f054 --- /dev/null +++ b/Tools/BiblioTech/DeploymentFilesMonitor.cs @@ -0,0 +1,47 @@ +using CodexPlugin; +using Newtonsoft.Json; + +namespace BiblioTech +{ + public class DeploymentFilesMonitor + { + private DateTime lastUpdate = DateTime.MinValue; + private CodexDeployment[] deployments = Array.Empty(); + + public CodexDeployment[] GetDeployments() + { + if (ShouldUpdate()) + { + UpdateDeployments(); + } + + return deployments; + } + + private void UpdateDeployments() + { + lastUpdate = DateTime.UtcNow; + var path = Program.Config.DeploymentsPath; + var files = Directory.GetFiles(path); + deployments = files.Select(ProcessFile).Where(d => d != null).Cast().ToArray(); + } + + private CodexDeployment? ProcessFile(string filename) + { + try + { + var lines = File.ReadAllLines(filename); + return JsonConvert.DeserializeObject(string.Join(" ", lines)); + } + catch + { + return null; + } + } + + private bool ShouldUpdate() + { + return !deployments.Any() || (DateTime.UtcNow - lastUpdate) > TimeSpan.FromMinutes(10); + } + } +} diff --git a/Tools/BiblioTech/HelloWorldCommand.cs b/Tools/BiblioTech/HelloWorldCommand.cs new file mode 100644 index 0000000..2a7ef6a --- /dev/null +++ b/Tools/BiblioTech/HelloWorldCommand.cs @@ -0,0 +1,61 @@ +using Discord; +using Discord.Net; +using Discord.WebSocket; +using Newtonsoft.Json; + +namespace BiblioTech +{ + public class HelloWorldCommand + { + private readonly DiscordSocketClient client; + + public HelloWorldCommand(DiscordSocketClient client) + { + this.client = client; + + client.Ready += Client_Ready; + } + + private async Task Client_Ready() + { + // Let's build a guild command! We're going to need a guild so lets just put that in a variable. + var guild = client.Guilds.Single(g => g.Name == "ThatBen's server"); + + // Next, lets create our slash command builder. This is like the embed builder but for slash commands. + var guildCommand = new SlashCommandBuilder(); + + // Note: Names have to be all lowercase and match the regular expression ^[\w-]{3,32}$ + guildCommand.WithName("do-thing"); + + // Descriptions can have a max length of 100. + guildCommand.WithDescription("This command does the thing!"); + + //// Let's do our global command + //var globalCommand = new SlashCommandBuilder(); + //globalCommand.WithName("first-global-command"); + //globalCommand.WithDescription("This is my first global slash command"); + + try + { + // Now that we have our builder, we can call the CreateApplicationCommandAsync method to make our slash command. + await guild.CreateApplicationCommandAsync(guildCommand.Build()); + + // With global commands we don't need the guild. + //await client.CreateGlobalApplicationCommandAsync(globalCommand.Build()); + // Using the ready event is a simple implementation for the sake of the example. Suitable for testing and development. + // For a production bot, it is recommended to only run the CreateGlobalApplicationCommandAsync() once for each command. + } + catch (ApplicationCommandException exception) + { + // If our command was invalid, we should catch an ApplicationCommandException. This exception contains the path of the error as well as the error message. You can serialize the Error field in the exception to get a visual of where your error is. + var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented); + + // You can send this error somewhere or just print it to the console, for this example we're just going to print it. + Console.WriteLine(json); + } + } + + + + } +} diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index cd35718..4950f26 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -1,6 +1,9 @@ using ArgsUniform; using Discord; +using Discord.Commands; +using Discord.Net; using Discord.WebSocket; +using Newtonsoft.Json; namespace BiblioTech { @@ -9,6 +12,7 @@ namespace BiblioTech private DiscordSocketClient client = null!; public static Configuration Config { get; private set; } = null!; + public static DeploymentFilesMonitor DeploymentFilesMonitor { get; } = new DeploymentFilesMonitor(); public static Task Main(string[] args) { @@ -22,14 +26,16 @@ namespace BiblioTech { Console.WriteLine("Starting Codex Discord Bot..."); client = new DiscordSocketClient(); - client.Log += Log; - // You can assign your bot token to a string, and pass that in to connect. - // This is, however, insecure, particularly if you plan to have your code hosted in a public repository. - var token = "token"; + var helloWorld = new HelloWorldCommand(client); - await client.LoginAsync(TokenType.Bot, token); + //var cmdService = new CommandService(); + //var handler = new CommandHandler(client, cmdService); + //await handler.InstallCommandsAsync(); + //Console.WriteLine("Command handler installed..."); + + await client.LoginAsync(TokenType.Bot, Config.ApplicationToken); await client.StartAsync(); Console.WriteLine("Running..."); await Task.Delay(-1); diff --git a/Tools/BiblioTech/docker/Dockerfile b/Tools/BiblioTech/docker/Dockerfile index 7f3844b..7b7d6c7 100644 --- a/Tools/BiblioTech/docker/Dockerfile +++ b/Tools/BiblioTech/docker/Dockerfile @@ -2,5 +2,6 @@ FROM mcr.microsoft.com/dotnet/sdk:7.0 WORKDIR app COPY ./Tools/BiblioTech ./Tools/BiblioTech -COPY ./Framework/ArgsUniform ./Framework/ArgsUniform +COPY ./Framework ./Framework +COPY ./ProjectPlugins ./ProjectPlugins CMD ["dotnet", "run", "--project", "Tools/BiblioTech"]