From e70d69e45c3e4d957e9690bc128827ed9eb9e22a Mon Sep 17 00:00:00 2001 From: dusk Date: Sun, 5 Jan 2025 00:58:48 +0900 Subject: [PATCH] refactor: start to stop using ctx.Match --- PluralKit.Bot/CommandMeta/CommandTree.cs | 21 ++++---- PluralKit.Bot/CommandSystem/ParametersFFI.cs | 5 ++ crates/commands/src/commands.rs | 2 +- crates/commands/src/commands/fun.rs | 9 ++++ crates/commands/src/lib.rs | 4 +- flake.nix | 50 ++++++++++---------- 6 files changed, 56 insertions(+), 35 deletions(-) diff --git a/PluralKit.Bot/CommandMeta/CommandTree.cs b/PluralKit.Bot/CommandMeta/CommandTree.cs index 3c620e63..2c11d02f 100644 --- a/PluralKit.Bot/CommandMeta/CommandTree.cs +++ b/PluralKit.Bot/CommandMeta/CommandTree.cs @@ -6,6 +6,19 @@ public partial class CommandTree { public Task ExecuteCommand(Context ctx) { + switch (ctx.Parameters.Callback()) + { + case "fun_thunder": + return ctx.Execute(null, m => m.Thunder(ctx)); + default: + // don't send an "invalid command" response if the guild has those turned off + if (ctx.GuildConfig != null && ctx.GuildConfig!.InvalidCommandResponseEnabled != true) + return Task.CompletedTask; + + // remove compiler warning + return ctx.Reply( + $"{Emojis.Error} Unknown command {ctx.PeekArgument().AsCode()}. For a list of possible commands, see ."); + } if (ctx.Match("system", "s")) return HandleSystemCommand(ctx); if (ctx.Match("member", "m")) @@ -107,14 +120,6 @@ public partial class CommandTree return ctx.Execute(MemberRandom, m => m.Member(ctx, ctx.System)); if (ctx.Match("dashboard", "dash")) return ctx.Execute(Dashboard, m => m.Dashboard(ctx)); - - // don't send an "invalid command" response if the guild has those turned off - if (ctx.GuildConfig != null && ctx.GuildConfig!.InvalidCommandResponseEnabled != true) - return Task.CompletedTask; - - // remove compiler warning - return ctx.Reply( - $"{Emojis.Error} Unknown command {ctx.PeekArgument().AsCode()}. For a list of possible commands, see ."); } private async Task HandleAdminAbuseLogCommand(Context ctx) diff --git a/PluralKit.Bot/CommandSystem/ParametersFFI.cs b/PluralKit.Bot/CommandSystem/ParametersFFI.cs index dcd1097b..474b5833 100644 --- a/PluralKit.Bot/CommandSystem/ParametersFFI.cs +++ b/PluralKit.Bot/CommandSystem/ParametersFFI.cs @@ -29,6 +29,11 @@ public class ParametersFFI } } + public string Callback() + { + return _cb; + } + public string Pop() { if (_args.Count > _ptr + 1) Console.WriteLine($"pop: {_ptr + 1}, {_args[_ptr + 1]}"); diff --git a/crates/commands/src/commands.rs b/crates/commands/src/commands.rs index b87dc878..789240ab 100644 --- a/crates/commands/src/commands.rs +++ b/crates/commands/src/commands.rs @@ -22,7 +22,7 @@ use smol_str::SmolStr; use crate::{command, token::Token}; -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Command { // TODO: fix hygiene pub tokens: Vec, diff --git a/crates/commands/src/commands/fun.rs b/crates/commands/src/commands/fun.rs index 8b137891..e14da01b 100644 --- a/crates/commands/src/commands/fun.rs +++ b/crates/commands/src/commands/fun.rs @@ -1 +1,10 @@ +use super::*; +pub fn cmds() -> impl Iterator { + [command!( + [Token::cmd("thunder")], + "fun_thunder", + "Shows the help command" + )] + .into_iter() +} diff --git a/crates/commands/src/lib.rs b/crates/commands/src/lib.rs index 4b701e1d..f2b4a969 100644 --- a/crates/commands/src/lib.rs +++ b/crates/commands/src/lib.rs @@ -1,6 +1,6 @@ #![feature(let_chains)] -mod commands; +pub mod commands; mod string; mod token; mod tree; @@ -17,7 +17,7 @@ pub use commands::Command; pub use token::*; lazy_static::lazy_static! { - static ref COMMAND_TREE: TreeBranch = { + pub static ref COMMAND_TREE: TreeBranch = { let mut tree = TreeBranch { current_command_key: None, possible_tokens: vec![], diff --git a/flake.nix b/flake.nix index dc5d11c3..a310bfeb 100644 --- a/flake.nix +++ b/flake.nix @@ -43,22 +43,6 @@ ... }: let - # this is used as devshell for bot, and in the process-compose processes as environment - mkBotEnv = - cmd: - pkgs.buildFHSEnv { - name = "env"; - targetPkgs = - pkgs: with pkgs; [ - coreutils - git - dotnet-sdk_8 - gcc - omnisharp-roslyn - bashInteractive - ]; - runScript = cmd; - }; uniffi-bindgen-cs = config.nci.lib.buildCrate { src = inp.uniffi-bindgen-cs; cratePath = "bindgen"; @@ -75,7 +59,7 @@ programs.nixfmt.enable = true; }; - nci.projects."pluralkit-services" = { + nci.projects."pk-services" = { path = ./.; export = false; }; @@ -118,9 +102,23 @@ packages = lib.genAttrs [ "gateway" "commands" ] (name: rustOutputs.${name}.packages.release); # TODO: package the bot itself (dotnet) - devShells = { - services = rustOutputs."pluralkit-services".devShell; - bot = (mkBotEnv "bash").env; + devShells = rec { + services = rustOutputs."pk-services".devShell; + bot = pkgs.mkShell { + name = "pkbot-devshell"; + nativeBuildInputs = with pkgs; [ + coreutils + git + dotnet-sdk_8 + gcc + omnisharp-roslyn + bashInteractive + ]; + }; + all = (pkgs.mkShell.override { stdenv = services.stdenv; }) { + name = "pk-devshell"; + nativeBuildInputs = bot.nativeBuildInputs ++ services.nativeBuildInputs; + }; }; process-compose."dev" = @@ -194,27 +192,31 @@ pluralkit-bot-init = { command = pkgs.writeShellApplication { name = "pluralkit-bot-init"; - runtimeInputs = [ + runtimeInputs = self'.devShells.bot.nativeBuildInputs ++ [ pkgs.coreutils pkgs.git + self'.devShells.bot.stdenv.cc ]; text = '' ${sourceDotenv} set -x ${pluralkitConfCheck} ${self'.apps.generate-command-parser-bindings.program} - exec ${mkBotEnv "dotnet build -c Release -o obj/"}/bin/env + exec dotnet build -c Release -o obj/ ''; }; }; pluralkit-bot = { command = pkgs.writeShellApplication { name = "pluralkit-bot"; - runtimeInputs = [ pkgs.coreutils ]; + runtimeInputs = self'.devShells.bot.nativeBuildInputs ++ [ + pkgs.coreutils + self'.devShells.bot.stdenv.cc + ]; text = '' ${sourceDotenv} set -x - exec ${mkBotEnv "dotnet obj/PluralKit.Bot.dll"}/bin/env + exec dotnet obj/PluralKit.Bot.dll ''; }; depends_on.pluralkit-bot-init.condition = "process_completed_successfully";