From 660a63ab53ce2cfd3a3fc1225dd7534fa3664403 Mon Sep 17 00:00:00 2001 From: Flonja <20887403+Flonja@users.noreply.github.com> Date: Tue, 10 Oct 2023 19:28:55 +0200 Subject: [PATCH 1/2] Allow developers to forcefully hide/show commands --- server/cmd/command.go | 12 ++++++++++++ server/session/command.go | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/server/cmd/command.go b/server/cmd/command.go index 9cb64d390..48ade2fc0 100644 --- a/server/cmd/command.go +++ b/server/cmd/command.go @@ -48,6 +48,7 @@ type Command struct { description string usage string aliases []string + visible bool } // New returns a new Command using the name and description passed. The Runnable passed must be a @@ -91,6 +92,12 @@ func New(name, description string, aliases []string, r ...Runnable) Command { return Command{name: name, description: description, aliases: aliases, v: runnableValues, usage: strings.Join(usages, "\n")} } +// WithAlwaysVisible sets the visible field of the Command struct to the passed value. +func (cmd Command) WithAlwaysVisible(alwaysVisible bool) Command { + cmd.visible = alwaysVisible + return cmd +} + // Name returns the name of the command. The name is guaranteed to be lowercase and will never have spaces in // it. This name is used to call the command, and is shown in the /help list. func (cmd Command) Name() string { @@ -115,6 +122,11 @@ func (cmd Command) Aliases() []string { return cmd.aliases } +// AlwaysVisible specifies if a command should still be visible even if Allower returned false +func (cmd Command) AlwaysVisible() bool { + return cmd.visible +} + // Execute executes the Command as a source with the args passed. The args are parsed assuming they do not // start with the command name. Execute will attempt to parse and execute one Runnable at a time. If one of // the Runnable was able to parse args correctly, it will be executed and no more Runnables will be attempted diff --git a/server/session/command.go b/server/session/command.go index 116456baa..f96b63a10 100644 --- a/server/session/command.go +++ b/server/session/command.go @@ -56,8 +56,10 @@ func (s *Session) sendAvailableCommands() map[string]map[int]cmd.Runnable { // Don't add duplicate entries for aliases. continue } - if run := c.Runnables(s.c); len(run) > 0 { + if run := c.Runnables(s.c); len(run) > 0 || c.AlwaysVisible() { m[alias] = run + } else { + continue } params := c.Params(s.c) @@ -201,7 +203,7 @@ func (s *Session) resendCommands(before map[string]map[int]cmd.Runnable) (map[st for alias, c := range commands { if c.Name() == alias { - if run := c.Runnables(s.c); len(run) > 0 { + if run := c.Runnables(s.c); len(run) > 0 || c.AlwaysVisible() { m[alias] = run } } From 96c0b273cea02867fb53e225782efb8d21c6a55d Mon Sep 17 00:00:00 2001 From: Flonja <20887403+Flonja@users.noreply.github.com> Date: Tue, 10 Oct 2023 19:59:01 +0200 Subject: [PATCH 2/2] Remove opt-in feature :( --- server/cmd/command.go | 12 ------------ server/session/command.go | 4 ++-- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/server/cmd/command.go b/server/cmd/command.go index 48ade2fc0..9cb64d390 100644 --- a/server/cmd/command.go +++ b/server/cmd/command.go @@ -48,7 +48,6 @@ type Command struct { description string usage string aliases []string - visible bool } // New returns a new Command using the name and description passed. The Runnable passed must be a @@ -92,12 +91,6 @@ func New(name, description string, aliases []string, r ...Runnable) Command { return Command{name: name, description: description, aliases: aliases, v: runnableValues, usage: strings.Join(usages, "\n")} } -// WithAlwaysVisible sets the visible field of the Command struct to the passed value. -func (cmd Command) WithAlwaysVisible(alwaysVisible bool) Command { - cmd.visible = alwaysVisible - return cmd -} - // Name returns the name of the command. The name is guaranteed to be lowercase and will never have spaces in // it. This name is used to call the command, and is shown in the /help list. func (cmd Command) Name() string { @@ -122,11 +115,6 @@ func (cmd Command) Aliases() []string { return cmd.aliases } -// AlwaysVisible specifies if a command should still be visible even if Allower returned false -func (cmd Command) AlwaysVisible() bool { - return cmd.visible -} - // Execute executes the Command as a source with the args passed. The args are parsed assuming they do not // start with the command name. Execute will attempt to parse and execute one Runnable at a time. If one of // the Runnable was able to parse args correctly, it will be executed and no more Runnables will be attempted diff --git a/server/session/command.go b/server/session/command.go index f96b63a10..f4813a2ee 100644 --- a/server/session/command.go +++ b/server/session/command.go @@ -56,7 +56,7 @@ func (s *Session) sendAvailableCommands() map[string]map[int]cmd.Runnable { // Don't add duplicate entries for aliases. continue } - if run := c.Runnables(s.c); len(run) > 0 || c.AlwaysVisible() { + if run := c.Runnables(s.c); len(run) > 0 { m[alias] = run } else { continue @@ -203,7 +203,7 @@ func (s *Session) resendCommands(before map[string]map[int]cmd.Runnable) (map[st for alias, c := range commands { if c.Name() == alias { - if run := c.Runnables(s.c); len(run) > 0 || c.AlwaysVisible() { + if run := c.Runnables(s.c); len(run) > 0 { m[alias] = run } }