AST Recipes & Patterns
Copy-ready AST templates for counters, conditionals, viewer lookups, timed actions, polls, and predictions, along with the plan rules and limits each one relies on.
Treat these recipes as ready-to-adapt starting points—each snippet is a full custom command reply (or a pair of commands where noted). Before opening any channel-changing recipe to chat, test it in a command restricted to yourself. For syntax rules see AST Syntax & Data, and for individual functions see AST Functions Reference.
Counters
Section titled “Counters”Count how many times something happened
Section titled “Count how many times something happened”*( ^(#wins) ? %(#wins *(%(#wins) + 1)) : %(#wins 1) )$(user) has %(#wins) wins!The first line initializes #wins to 1 if it doesn’t exist yet and increments it by 1 on every run after that; the second line posts the updated total.
- Cache variables (
#) expire 24 hours after their last write. If a full day passes without anyone running the command, the count resets. - When the count needs to last permanently, switch to a database variable:
%(*wins ...)for a channel-wide counter, or%(**wins ...)for a per-viewer counter. Database writes need Premium or Pro. On Free, writes are skipped silently without an error. See Execution, Errors & Limits.
Look up another viewer’s value
Section titled “Look up another viewer’s value”%(##rank(&p1))Because &p1 captures the first word after the command, !rank ronchi reads the value stored for the login ronchi. Omit the selector and ## or ** variables automatically target whoever ran the command.
Conditions
Section titled “Conditions”Choose one of two replies
Section titled “Choose one of two replies”*( %(#ready) == 1 ? "Ready" : "Not ready" )A ternary *( condition ? yes : no ) only evaluates the matching branch, so keep any channel-changing action strictly inside the branch where it belongs.
Run an action only when a condition is true
Section titled “Run an action only when a condition is true”*( %(#ready) == 1 ? "Ready" : "" )Leaving the false branch as an empty string ensures nothing prints when the condition isn’t met.
Timed actions
Section titled “Timed actions”Pause a command for a set time
Section titled “Pause a command for a set time”This temporarily turns !spoilers off for five minutes and then restores whatever state it had before:
$(disable.command spoilers 300)!spoilers is paused for five minutes.Durations are given in seconds from 1 to 604800, and the command restores to its prior state even if the bot restarts in the meantime. See Availability functions.
Send a message and an action later
Section titled “Send a message and an action later”The timer finishes immediately in the outer command while scheduling its message and action for five minutes later—and it can survive a bot restart:
$(timer 300 "Five minutes are up!" $(trigger.send airhorn))Every timer executes at most once and expires if it reaches 10 minutes late; execution is not guaranteed if a failure happens after the timer is claimed. See Execution, Errors & Limits.
Prediction results
Section titled “Prediction results”Split prediction resolution across two commands so a moderator can review the numbered outcomes before picking a winner. Set both commands to Moderators access.
Command 1, !predictions: displays the question along with its numbered choices.
$(get.prediction)Command 2, !winner: resolves the prediction using either the outcome’s title or its number.
$(end.prediction &t)A moderator can then run !winner Yes or !winner 1. Using &t preserves multi-word outcome titles. Every time it runs, end.prediction fetches the current Twitch prediction and refuses both non-existent numbers and titles that match more than one choice.
Poll results
Section titled “Poll results”Break a tie with the wheel
Section titled “Break a tie with the wheel”Run this command once—or create a roulette with ID polls on the Roulette page—and add the wheel to an OBS overlay:
$(roulette.create polls "Poll tie-breaker")Next, paste this script into your Poll ended event message:
*($(poll.tied) ? "${$(roulette.fill polls $(poll.tied))}${$(roulette.start polls)}Tie! The wheel decides." : "${$(poll.result)} won the poll!")When the poll ties, the tied choices replace the roulette items and the wheel spins; otherwise, chat gets the winner right away. Inside double quotes, ${ } evaluates each function, and since roulette.fill and roulette.start produce no output, only the chat sentence is sent.
Announce the wheel winner later
Section titled “Announce the wheel winner later”This version also announces the wheel’s winner eight seconds later. Set the timer longer than the spin duration (four seconds by default), and leave the timer text unquoted so roulette.result evaluates when the timer fires rather than immediately:
*($(poll.tied) ? "${$(roulette.fill polls $(poll.tied))}${$(roulette.start polls)}${$(timer 8 The wheel picked $(roulette.result polls)!)}Tie! The wheel decides." : "${$(poll.result)} won the poll!")Handle a poll with no votes
Section titled “Handle a poll with no votes”When a poll ends with zero votes, every option counts as tied. Check poll.total first to handle that case cleanly:
*($(poll.total) == 0 ? "Nobody voted." : *($(poll.tied) ? "${$(roulette.fill polls $(poll.tied))}${$(roulette.start polls)}Tie! The wheel decides." : "${$(poll.result)} won the poll!"))Make a wheel weighted by votes
Section titled “Make a wheel weighted by votes”Restrict this command to moderators. Every vote becomes one slice on the wheel, and options with zero votes are skipped:
$(roulette.clear polls)*(for #o in $(poll.options) { %(ids[] $(roulette.add polls #o 1 $(poll.votes #o))) })$(roulette.start polls)Every vote is a slice. Spinning!Say who is ahead
Section titled “Say who is ahead”Use this command to announce the current leader while a poll is live. Because poll.result is blank during a tie, check poll.tied first:
*($(poll.tied) ? "It is a tie so far." : "${$(poll.result)} leads with ${$(poll.votes $(poll.result))} of ${$(poll.total)} votes.")Split a large command into smaller ones
Section titled “Split a large command into smaller ones”If a command grows hard to maintain, break pieces into separate commands and chain them with #(commandName args...):
Welcome $(user)!#(rules)In this setup, !welcome posts its greeting first, and then !rules runs and posts its own reply. Whenever an argument includes punctuation such as ;, ?, or /, wrap it in quotes—for example #(poll "Best map?;Nuke/Mirage")—so those characters aren’t altered along the way.
Keep these rules in mind:
- Chains can nest up to five levels deep, with a cap of 50 total calls per run.
- A command cannot call itself, either directly or indirectly through another command.
- Called commands don’t check their own access setting when triggered from one of your commands—only the calling command’s access is checked, so set the outer command’s access carefully.
- Every called command sends its own separate reply after the outer reply.
See Execution, Errors & Limits for the full rules.
Pattern summary
Section titled “Pattern summary”| Pattern | Syntax | Purpose |
|---|---|---|
| Count or start a count | *(^(#x) ? %(#x *(%(#x) + 1)) : %(#x 1)) | Initialize a missing counter or add 1 |
| Viewer lookup | %(##rank(&p1)) | Read a cached value saved for a specific viewer |
| Persistent viewer value | %(**rank(&p1)) | Read a viewer’s database value (requires Premium or Pro to save) |
| Condition | *(condition ? YES : NO) | Pick one of two outputs |
| Conditional action | *(condition ? ACTION : "") | Run an action only when a condition is true |
| Timed action | $(timer seconds "message" $(action)) | Schedule a message and action to run later, at most once |
| Poll results | poll.result, poll.tied | Check live/final poll winners or break ties with a wheel |
| Call another command | #(commandName args...) | Trigger a follow-up command after the main reply |