diff --git a/bot/ctx.go b/bot/ctx.go index 2567e6c..802b30a 100644 --- a/bot/ctx.go +++ b/bot/ctx.go @@ -281,6 +281,8 @@ func (ctx *Context) HelpAdmin() string { } func (ctx *Context) help(hideAdmin bool) string { + const indent = " " + var help strings.Builder // Generate the headers and descriptions @@ -291,7 +293,7 @@ func (ctx *Context) help(hideAdmin bool) string { } if ctx.Description != "" { - help.WriteString("\n " + ctx.Description) + help.WriteString("\n" + indent + ctx.Description) } if ctx.Flag.Is(AdminOnly) { @@ -310,7 +312,7 @@ func (ctx *Context) help(hideAdmin bool) string { continue } - help.WriteString(" " + ctx.Prefix + cmd.Command) + help.WriteString(indent + ctx.Prefix + cmd.Command) switch { case len(cmd.Usage()) > 0: @@ -326,34 +328,8 @@ func (ctx *Context) help(hideAdmin bool) string { var subcommands = ctx.Subcommands() for _, sub := range subcommands { - if sub.Flag.Is(AdminOnly) && hideAdmin { - continue - } - - subHelp.WriteString(" " + sub.Command) - - if sub.Description != "" { - subHelp.WriteString(": " + sub.Description) - } - - subHelp.WriteByte('\n') - - for _, cmd := range sub.Commands { - if cmd.Flag.Is(AdminOnly) && hideAdmin { - continue - } - - subHelp.WriteString(" " + - ctx.Prefix + sub.Command + " " + cmd.Command) - - switch { - case len(cmd.Usage()) > 0: - subHelp.WriteString(" " + strings.Join(cmd.Usage(), " ")) - case cmd.Description != "": - subHelp.WriteString(": " + cmd.Description) - } - - subHelp.WriteByte('\n') + if help := sub.Help(ctx.Prefix, indent, hideAdmin); help != "" { + subHelp.WriteString(help) } } diff --git a/bot/subcommand.go b/bot/subcommand.go index 5013a78..4235d2b 100644 --- a/bot/subcommand.go +++ b/bot/subcommand.go @@ -154,6 +154,49 @@ func (sub *Subcommand) ChangeCommandInfo(methodName, cmd, desc string) bool { return false } +func (sub *Subcommand) Help(prefix, indent string, hideAdmin bool) string { + if sub.Flag.Is(AdminOnly) && hideAdmin { + return "" + } + + var subHelp string + if sub.Command != "" { + subHelp += indent + sub.Command + } + + if sub.Description != "" { + if subHelp != "" { + subHelp += ": " + } else { + subHelp += indent + } + + subHelp += sub.Description + } + + subHelp += "\n" + + for _, cmd := range sub.Commands { + if cmd.Flag.Is(AdminOnly) && hideAdmin { + continue + } + + subHelp += indent + indent + + prefix + sub.Command + " " + cmd.Command + + switch { + case len(cmd.Usage()) > 0: + subHelp += " " + strings.Join(cmd.Usage(), " ") + case cmd.Description != "": + subHelp += ": " + cmd.Description + } + + subHelp += "\n" + } + + return subHelp +} + func (sub *Subcommand) reflectCommands() error { t := reflect.TypeOf(sub.command) v := reflect.ValueOf(sub.command)