refactor: start to stop using ctx.Match

This commit is contained in:
dusk 2025-01-05 00:58:48 +09:00
parent b89bc44a27
commit e70d69e45c
No known key found for this signature in database
6 changed files with 56 additions and 35 deletions

View file

@ -6,6 +6,19 @@ public partial class CommandTree
{ {
public Task ExecuteCommand(Context ctx) public Task ExecuteCommand(Context ctx)
{ {
switch (ctx.Parameters.Callback())
{
case "fun_thunder":
return ctx.Execute<Fun>(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 <https://pluralkit.me/commands>.");
}
if (ctx.Match("system", "s")) if (ctx.Match("system", "s"))
return HandleSystemCommand(ctx); return HandleSystemCommand(ctx);
if (ctx.Match("member", "m")) if (ctx.Match("member", "m"))
@ -107,14 +120,6 @@ public partial class CommandTree
return ctx.Execute<Random>(MemberRandom, m => m.Member(ctx, ctx.System)); return ctx.Execute<Random>(MemberRandom, m => m.Member(ctx, ctx.System));
if (ctx.Match("dashboard", "dash")) if (ctx.Match("dashboard", "dash"))
return ctx.Execute<Help>(Dashboard, m => m.Dashboard(ctx)); return ctx.Execute<Help>(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 <https://pluralkit.me/commands>.");
} }
private async Task HandleAdminAbuseLogCommand(Context ctx) private async Task HandleAdminAbuseLogCommand(Context ctx)

View file

@ -29,6 +29,11 @@ public class ParametersFFI
} }
} }
public string Callback()
{
return _cb;
}
public string Pop() public string Pop()
{ {
if (_args.Count > _ptr + 1) Console.WriteLine($"pop: {_ptr + 1}, {_args[_ptr + 1]}"); if (_args.Count > _ptr + 1) Console.WriteLine($"pop: {_ptr + 1}, {_args[_ptr + 1]}");

View file

@ -22,7 +22,7 @@ use smol_str::SmolStr;
use crate::{command, token::Token}; use crate::{command, token::Token};
#[derive(Clone)] #[derive(Clone, Debug)]
pub struct Command { pub struct Command {
// TODO: fix hygiene // TODO: fix hygiene
pub tokens: Vec<Token>, pub tokens: Vec<Token>,

View file

@ -1 +1,10 @@
use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
[command!(
[Token::cmd("thunder")],
"fun_thunder",
"Shows the help command"
)]
.into_iter()
}

View file

@ -1,6 +1,6 @@
#![feature(let_chains)] #![feature(let_chains)]
mod commands; pub mod commands;
mod string; mod string;
mod token; mod token;
mod tree; mod tree;
@ -17,7 +17,7 @@ pub use commands::Command;
pub use token::*; pub use token::*;
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref COMMAND_TREE: TreeBranch = { pub static ref COMMAND_TREE: TreeBranch = {
let mut tree = TreeBranch { let mut tree = TreeBranch {
current_command_key: None, current_command_key: None,
possible_tokens: vec![], possible_tokens: vec![],

View file

@ -43,22 +43,6 @@
... ...
}: }:
let 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 { uniffi-bindgen-cs = config.nci.lib.buildCrate {
src = inp.uniffi-bindgen-cs; src = inp.uniffi-bindgen-cs;
cratePath = "bindgen"; cratePath = "bindgen";
@ -75,7 +59,7 @@
programs.nixfmt.enable = true; programs.nixfmt.enable = true;
}; };
nci.projects."pluralkit-services" = { nci.projects."pk-services" = {
path = ./.; path = ./.;
export = false; export = false;
}; };
@ -118,9 +102,23 @@
packages = lib.genAttrs [ "gateway" "commands" ] (name: rustOutputs.${name}.packages.release); packages = lib.genAttrs [ "gateway" "commands" ] (name: rustOutputs.${name}.packages.release);
# TODO: package the bot itself (dotnet) # TODO: package the bot itself (dotnet)
devShells = { devShells = rec {
services = rustOutputs."pluralkit-services".devShell; services = rustOutputs."pk-services".devShell;
bot = (mkBotEnv "bash").env; 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" = process-compose."dev" =
@ -194,27 +192,31 @@
pluralkit-bot-init = { pluralkit-bot-init = {
command = pkgs.writeShellApplication { command = pkgs.writeShellApplication {
name = "pluralkit-bot-init"; name = "pluralkit-bot-init";
runtimeInputs = [ runtimeInputs = self'.devShells.bot.nativeBuildInputs ++ [
pkgs.coreutils pkgs.coreutils
pkgs.git pkgs.git
self'.devShells.bot.stdenv.cc
]; ];
text = '' text = ''
${sourceDotenv} ${sourceDotenv}
set -x set -x
${pluralkitConfCheck} ${pluralkitConfCheck}
${self'.apps.generate-command-parser-bindings.program} ${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 = { pluralkit-bot = {
command = pkgs.writeShellApplication { command = pkgs.writeShellApplication {
name = "pluralkit-bot"; name = "pluralkit-bot";
runtimeInputs = [ pkgs.coreutils ]; runtimeInputs = self'.devShells.bot.nativeBuildInputs ++ [
pkgs.coreutils
self'.devShells.bot.stdenv.cc
];
text = '' text = ''
${sourceDotenv} ${sourceDotenv}
set -x 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"; depends_on.pluralkit-bot-init.condition = "process_completed_successfully";