Added support for named counters, getcount placeholder, and nested placeholders.

2026-02-09 21:47:56 -05:00
parent 4be0412616
commit 4170fd388b
2 changed files with 54 additions and 5 deletions
+8 -3
@@ -86,8 +86,13 @@ Modules may add their own config keys under their name. These appear in the conf
```yaml
modules:
my_module:
custom_commands:
enabled: true
greeting: "Hello"
max_retries: 3
max_nesting_depth: 4
```
#### Custom Commands
| Key | Default | Description |
|-----|---------|-------------|
| `max_nesting_depth` | `4` | Maximum nesting depth for placeholders. `$(` tokens encountered at or beyond this depth are treated as literal text. |
+46 -2
@@ -54,16 +54,40 @@ Custom command responses support placeholders that are replaced at runtime.
|-------------|-------------|---------|
| `$(1)` through `$(9)` | Command arguments by position. Empty string if not provided. | `!greet $(1)` with `!greet Alice` produces `Alice` |
| `$(count)` | Number of times the command has been used (incremented before display). | `This command has been used $(count) times.` |
| `$(count name)` | Increment a named counter by 1 and display its value. Supports modifiers: `+n` (increment), `-n` (decrement), or `n` (set). | `$(count deaths)` increments and shows the `deaths` counter. `$(count deaths +5)` increments by 5. `$(count deaths 0)` resets to 0. |
| `$(getcount name)` | Display a named counter's value without changing it. Returns `0` if the counter doesn't exist. | `$(getcount deaths)` shows the current value of the `deaths` counter. |
| `$(user)` | Display name of the user who ran the command. | `Hello, $(user)!` |
| `$(rand start stop)` | Random integer between `start` and `stop` (inclusive). | `$(rand 1 100)` produces `42` |
| `$(countdown date)` | Human-readable time remaining until a date. | `$(countdown Dec 25 2025 12:00:00 AM EST)` produces `3 days 4 hours` |
| `$(countup date)` | Human-readable time elapsed since a date. | `$(countup Jan 1 2024 12:00:00 AM UTC)` produces `365 days 3 hours` |
| `\$(...)` | Escaped. Outputs the literal placeholder text without expanding it. | `\$(count)` produces `$(count)` |
Placeholders are validated when a command is created or edited. If a placeholder has invalid syntax (e.g., `$(rand abc)` or `$(countdown bad date)`), the command is rejected with an error message.
If a placeholder has invalid arguments at runtime (e.g., `$(rand abc)` or `$(getcount)` with no name), the entire response is replaced with an error message describing the problem.
Unknown placeholder names are left as-is in the output.
### Nesting
Placeholders can be nested inside the arguments of other placeholders. Inner placeholders are resolved first, and their results are passed as arguments to the outer placeholder.
```
!addcommand !customrand $(rand $(1) $(2))
```
Now `!customrand 1 10` produces a random number between 1 and 10. The inner `$(1)` and `$(2)` resolve to the user's arguments, then `$(rand 1 10)` produces the final result.
Multi-level nesting also works:
```
!addcommand !deeprand $(rand 1 $(rand 5 10))
```
This picks a random number between 5 and 10, then picks a random number between 1 and that result.
Nesting is limited to a configurable maximum depth (default 4). Beyond the limit, `$(` is treated as literal text. The placeholder name itself must always be literal text. Constructions like `$($(1))` are not valid.
Escaping applies to the entire placeholder group including any nested content. `\$(rand $(1) $(2))` outputs the literal text `$(rand $(1) $(2))` without resolving anything inside.
### Date Format
`$(countdown)` and `$(countup)` use the following date format:
@@ -82,7 +106,7 @@ If the target date is in the past for `$(countdown)`, it returns "0 seconds". Th
Cooldowns are global per-command, not per-user. Setting `!commandcooldown !hello 30` means `!hello` can only fire once every 30 seconds regardless of who invokes it. A cooldown of 0 disables it.
New commands default to a 5-second cooldown.
New commands default to a 5-second cooldown. This default can be changed via the `default_cooldown` module configuration option.
When a command is on cooldown, the user receives a private system message with the remaining wait time.
@@ -138,6 +162,26 @@ The module serves a command list page at `/owlbot/custom_commands/list` showing
!addalias !shoutout !so
!commandcooldown !shoutout 10
# Named death counter (increments each use)
!addcommand !deaths We have died $(count deaths) times.
# Display death count without incrementing
!addcommand !deathcheck Total deaths so far: $(getcount deaths)
# Reset a named counter
!editcommand !deaths $(count deaths 0) deaths. Counter reset!
# Multiple named counters in one command
!addcommand !score Wins: $(getcount wins) | Losses: $(getcount losses)
!addcommand !win We won! Total wins: $(count wins)
!addcommand !loss We lost. Total losses: $(count losses)
# Nested placeholders: random range from arguments
!addcommand !customrand $(rand $(1) $(2))
# Multi-level nesting
!addcommand !deeprand $(rand 1 $(rand 5 10))
# Show literal placeholder text (escaped)
!addcommand !placeholderhelp Use \$(user) to insert your name.
```