Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

Add help.go for the unified !help command (missed in earlier commit)

Gabe Kangas75e35c84f4b0
diff --git a/services/plugins/help.go b/services/plugins/help.go
new file mode 100644
index 0000000000..2edc322954
--- /dev/null
+++ b/services/plugins/help.go
@@ -0,0 +1,96 @@
+package plugins
+
+import (
+ "sort"
+ "strings"
+)
+
+// HelpCommand is the chat command that triggers the unified command listing.
+// The host owns it (no plugin needs to implement it); a plugin can't shadow it.
+const HelpCommand = "!help"
+
+// IsHelpCommand reports whether a chat message body is the help command. Matched
+// case-insensitively and trim-tolerant; "!commands" is accepted as an alias.
+func IsHelpCommand(body string) bool {
+ b := strings.ToLower(strings.TrimSpace(body))
+ return b == HelpCommand || b == "!commands"
+}
+
+// BuildHelpMessage renders the unified command list aggregated across the given
+// loaded plugins, for a viewer whose moderator status is isModerator. Commands
+// are grouped by plugin (so the source is clear) and sorted; mod-only commands
+// are hidden from non-moderators. Returns "" when there's nothing to show, so
+// the caller can stay silent rather than post an empty list.
+func BuildHelpMessage(loaded []*Loaded, isModerator bool) string {
+ type group struct {
+ plugin string
+ lines []string
+ }
+ var groups []group
+
+ // Stable plugin order by display name (fallback slug).
+ sorted := append([]*Loaded(nil), loaded...)
+ sort.SliceStable(sorted, func(i, j int) bool {
+ return helpPluginName(sorted[i]) < helpPluginName(sorted[j])
+ })
+
+ for _, l := range sorted {
+ if l == nil || l.Manifest == nil {
+ continue
+ }
+ cmds := append([]CommandInfo(nil), l.Manifest.Commands...)
+ sort.SliceStable(cmds, func(i, j int) bool { return cmds[i].Name < cmds[j].Name })
+
+ var lines []string
+ for _, c := range cmds {
+ if c.ModOnly && !isModerator {
+ continue
+ }
+ prefix := c.Prefix
+ if prefix == "" {
+ prefix = "!"
+ }
+ // Markdown: backticked command, em-dash description. System
+ // messages are rendered (RenderBody), so this becomes a code span
+ // + text; it also degrades to readable plaintext if unrendered.
+ line := "- `" + prefix + c.Name + "`"
+ if c.Description != "" {
+ line += " — " + c.Description
+ }
+ if c.ModOnly {
+ line += " _(mod)_"
+ }
+ lines = append(lines, line)
+ }
+ if len(lines) > 0 {
+ groups = append(groups, group{plugin: helpPluginName(l), lines: lines})
+ }
+ }
+
+ if len(groups) == 0 {
+ return ""
+ }
+
+ // Rendered as markdown: a header paragraph, then per-plugin a bold heading
+ // followed by a bulleted list (blank lines separate the blocks so goldmark
+ // renders the list, not a run-on line).
+ var b strings.Builder
+ b.WriteString("Available chat commands:")
+ for _, g := range groups {
+ b.WriteString("\n\n**")
+ b.WriteString(g.plugin)
+ b.WriteString("**\n\n")
+ b.WriteString(strings.Join(g.lines, "\n"))
+ }
+ return b.String()
+}
+
+func helpPluginName(l *Loaded) string {
+ if l == nil || l.Manifest == nil {
+ return ""
+ }
+ if l.Manifest.DisplayName != "" {
+ return l.Manifest.DisplayName
+ }
+ return l.Manifest.Slug
+}
diff --git a/services/plugins/help_test.go b/services/plugins/help_test.go
new file mode 100644
index 0000000000..20b888d743
--- /dev/null
+++ b/services/plugins/help_test.go
@@ -0,0 +1,80 @@
+package plugins
+
+import (
+ "strings"
+ "testing"
+)
+
+func loadedWithCommands(slug, display string, cmds []CommandInfo) *Loaded {
+ return &Loaded{Manifest: &Manifest{Slug: slug, DisplayName: display, Commands: cmds}}
+}
+
+func TestIsHelpCommand(t *testing.T) {
+ for _, b := range []string{"!help", " !help ", "!HELP", "!commands"} {
+ if !IsHelpCommand(b) {
+ t.Errorf("IsHelpCommand(%q) = false, want true", b)
+ }
+ }
+ for _, b := range []string{"help", "!helpme", "!uptime", ""} {
+ if IsHelpCommand(b) {
+ t.Errorf("IsHelpCommand(%q) = true, want false", b)
+ }
+ }
+}
+
+func TestBuildHelpMessageEmpty(t *testing.T) {
+ if got := BuildHelpMessage(nil, false); got != "" {
+ t.Errorf("no plugins should yield empty help, got %q", got)
+ }
+ // A plugin with no commands also yields nothing.
+ if got := BuildHelpMessage([]*Loaded{loadedWithCommands("x", "X", nil)}, false); got != "" {
+ t.Errorf("plugin without commands should yield empty help, got %q", got)
+ }
+}
+
+func TestBuildHelpMessageAggregatesAndGroups(t *testing.T) {
+ plugins := []*Loaded{
+ loadedWithCommands("stream-tracker", "Stream Tracker", []CommandInfo{
+ {Name: "who", Prefix: "!", Description: "Who's in chat"},
+ {Name: "uptime", Prefix: "!", Description: "Stream uptime"},
+ }),
+ loadedWithCommands("timer-bot", "Timer Bot", []CommandInfo{
+ {Name: "remind", Prefix: "!", Description: "Set a reminder"},
+ }),
+ }
+ out := BuildHelpMessage(plugins, false)
+ for _, want := range []string{"Stream Tracker", "Timer Bot", "`!uptime`", "`!who`", "`!remind`", "Stream uptime"} {
+ if !strings.Contains(out, want) {
+ t.Errorf("help output missing %q\n---\n%s", want, out)
+ }
+ }
+ // Grouped + sorted: Stream Tracker before Timer Bot; within it uptime before who.
+ if strings.Index(out, "Stream Tracker") > strings.Index(out, "Timer Bot") {
+ t.Error("plugins not sorted by display name")
+ }
+ if strings.Index(out, "`!uptime`") > strings.Index(out, "`!who`") {
+ t.Error("commands not sorted within a plugin")
+ }
+}
+
+func TestBuildHelpMessageModOnlyHiddenFromNonMods(t *testing.T) {
+ plugins := []*Loaded{
+ loadedWithCommands("mod", "Mod Tools", []CommandInfo{
+ {Name: "ban", Prefix: "!", Description: "Ban a user", ModOnly: true},
+ {Name: "rules", Prefix: "!", Description: "Show the rules"},
+ }),
+ }
+ // Non-moderator: ban hidden, rules shown.
+ nonMod := BuildHelpMessage(plugins, false)
+ if strings.Contains(nonMod, "ban") {
+ t.Errorf("mod-only command leaked to non-moderator:\n%s", nonMod)
+ }
+ if !strings.Contains(nonMod, "`!rules`") {
+ t.Errorf("non-mod command missing for non-moderator:\n%s", nonMod)
+ }
+ // Moderator: both shown, ban tagged.
+ mod := BuildHelpMessage(plugins, true)
+ if !strings.Contains(mod, "`!ban`") || !strings.Contains(mod, "(mod)") {
+ t.Errorf("moderator should see tagged mod-only command:\n%s", mod)
+ }
+}