From 4f533eac6be6b2d593926f5f30cb00423d49e43a Mon Sep 17 00:00:00 2001 From: Elara6331 Date: Wed, 24 Apr 2024 06:30:31 -0700 Subject: [PATCH] Rename /plugin to /pluginadm and make /prun and /phelp subcommands of /plugin instead --- internal/systems/plugins/commands.go | 30 +++++++++---- internal/systems/plugins/handlers.go | 4 +- internal/systems/plugins/init.go | 65 +++++++++++++++------------- 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/internal/systems/plugins/commands.go b/internal/systems/plugins/commands.go index cae584d..ff60fa7 100644 --- a/internal/systems/plugins/commands.go +++ b/internal/systems/plugins/commands.go @@ -11,8 +11,8 @@ import ( "go.elara.ws/owobot/internal/util" ) -// pluginCmd handles the `/plugin` command and routes it to the correct subcommand. -func pluginCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { +// pluginadmCmd handles the `/plugin` command and routes it to the correct subcommand. +func pluginadmCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { data := i.ApplicationCommandData() switch name := data.Options[0].Name; name { case "list": @@ -22,7 +22,7 @@ func pluginCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { case "disable": return disableCmd(s, i) default: - return fmt.Errorf("unknown plugin subcommand: %s", name) + return fmt.Errorf("unknown pluginadm subcommand: %s", name) } } @@ -107,10 +107,22 @@ func disableCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { return util.RespondEphemeral(s, i.Interaction, fmt.Sprintf("Successfully disabled the %q plugin", pluginName)) } -// phelpCmd handles the `/phelp` command. -func phelpCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { +func pluginCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { data := i.ApplicationCommandData() - cmdStr := data.Options[0].StringValue() + switch name := data.Options[0].Name; name { + case "run": + return pluginRunCmd(s, i) + case "help": + return pluginHelpCmd(s, i) + default: + return fmt.Errorf("unknown plugin subcommand: %s", name) + } +} + +// pluginHelpCmd handles the `/phelp` command. +func pluginHelpCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { + data := i.ApplicationCommandData() + cmdStr := data.Options[0].Options[0].StringValue() args, err := shellquote.Split(cmdStr) if err != nil { @@ -176,10 +188,10 @@ func phelpCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { return fmt.Errorf("command not found: %q", args[0]) } -// prunCmd handles the `/prunCmd` command. -func prunCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { +// pluginRunCmd handles the `/pluginRunCmd` command. +func pluginRunCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { data := i.ApplicationCommandData() - cmdStr := data.Options[0].StringValue() + cmdStr := data.Options[0].Options[0].StringValue() args, err := shellquote.Split(cmdStr) if err != nil { diff --git a/internal/systems/plugins/handlers.go b/internal/systems/plugins/handlers.go index 6a066a8..638631b 100644 --- a/internal/systems/plugins/handlers.go +++ b/internal/systems/plugins/handlers.go @@ -70,11 +70,11 @@ func handleAutocomplete(s *discordgo.Session, i *discordgo.InteractionCreate) { } data := i.ApplicationCommandData() - if data.Name != "prun" && data.Name != "phelp" { + if data.Name != "plugin" { return } - cmdStr := data.Options[0].StringValue() + cmdStr := data.Options[0].Options[0].StringValue() s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionApplicationCommandAutocompleteResult, diff --git a/internal/systems/plugins/init.go b/internal/systems/plugins/init.go index a1c6e46..95f257b 100644 --- a/internal/systems/plugins/init.go +++ b/internal/systems/plugins/init.go @@ -22,36 +22,43 @@ func Init(s *discordgo.Session) error { return err } - commands.Register(s, prunCmd, &discordgo.ApplicationCommand{ - Name: "prun", - Description: "Run a plugin command", - Options: []*discordgo.ApplicationCommandOption{ - { - Type: discordgo.ApplicationCommandOptionString, - Name: "cmd", - Description: "The plugin command to run", - Required: true, - Autocomplete: true, - }, - }, - }) - - commands.Register(s, phelpCmd, &discordgo.ApplicationCommand{ - Name: "phelp", - Description: "Display help for a plugin command", - Options: []*discordgo.ApplicationCommandOption{ - { - Type: discordgo.ApplicationCommandOptionString, - Name: "cmd", - Description: "The plugin command to display help for", - Required: true, - Autocomplete: true, - }, - }, - }) - commands.Register(s, pluginCmd, &discordgo.ApplicationCommand{ - Name: "plugin", + Name: "plugin", + Description: "Interact with the plugins on this server", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionSubCommand, + Name: "run", + Description: "Run a plugin command", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "cmd", + Description: "The plugin command to run", + Required: true, + Autocomplete: true, + }, + }, + }, + { + Type: discordgo.ApplicationCommandOptionSubCommand, + Name: "help", + Description: "See how to use a plugin command", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "cmd", + Description: "The plugin command to help with", + Required: true, + Autocomplete: true, + }, + }, + }, + }, + }) + + commands.Register(s, pluginadmCmd, &discordgo.ApplicationCommand{ + Name: "pluginadm", Description: "Manage dynamic plugins for your server", DefaultMemberPermissions: util.Pointer[int64](discordgo.PermissionManageServer), Options: []*discordgo.ApplicationCommandOption{