Build a small shared hug counter: a viewer types !hugs, the script reads the saved count, adds one, and replies with the result.
It demonstrates how a script can keep information between runs and use it in a chat response.
The calls come from Chat, music and voice and Data and storage.
Before you start
Custom Code must be on for the channel. It is off for a new channel. The channel’s plan must also include script time. Without it, the Scripts page shows “Code scripts need the Base plan.” Code scripts shows where you switch it on.
The script
const key = 'hugs.count';
const stored = nnz.api.storage.get(key);
const count = Number(stored || '0') + 1;
const saved = nnz.api.storage.set(key, String(count));
if (!saved) {
nnz.api.chat.reply('The count was not saved: ' + nnz.lastError.message);
} else {
nnz.api.chat.reply('Hugs given in this channel: ' + count);
}
| Line | What it does |
|---|---|
nnz.api.storage.get(key) | Reads the stored count as text, or gives null when nothing is stored yet. |
Number(stored || '0') + 1 | Turns the text into a number and adds one. The first run gives 1. |
nnz.api.storage.set(key, String(count)) | Stores the new count as text. It gives true when the value is stored, and false when it is refused. |
nnz.api.chat.reply(...) | Answers the viewer’s !hugs message as a threaded reply. |
The count belongs to the channel, so every viewer adds to the same number. The script sends the count only after storage accepts it. If the write fails, it sends the reason instead, so viewers are not told that an unsaved count was recorded.
This is a read-and-write example, not an atomic counter. Simultaneous runs can read the same old value and overwrite an increment, so use it for learning rather than a total that must be exact under concurrent use.
1. Create the script
- Open Scripts in the sidebar and choose New script.
- Type
hugsin Name and choose Create script. The new script opens beside the list. - Choose Edit code. The code editor opens.
- Replace the example code with the script above.
- Choose Save & Compile. The bot checks and builds the code and saves it as the next version. A clean save becomes the version the script runs.
Leave the script switched off while you test it. The test card can run the saved version without enabling live triggers.
When you save, the bot reads the calls the code names and keeps their grants with the version.
This script gets three grants: storage.get, storage.set and chat.reply.
It needs nothing more.
2. Test run it
- On the script’s Test card, choose Run test. The event picker shows only for a script that reads trigger values, so this script has none. The card shows Worked with the time in milliseconds.
A test run is a dry run. Nothing it does reaches chat. The read of the count runs for real. The store and the reply are only recorded, so the stored count does not change.
Timeline lists what the run did:
| Step | What you see |
|---|---|
| Effect | “Save values: hugs.count | 1”. The key and the value are joined by a bar. It was recorded, not stored. |
| Chat | The reply line the bot would post. |
3. Run it from chat
- On Scripts, switch
hugson. New scripts start off, so this is required for a live command to run it. - Open Commands in the sidebar and choose New command.
- Type
hugsin Name. - Choose Runs a code script in What it does.
- Under Script, pick
hugs. - Choose Create command. The command is switched on.
A viewer now types !hugs in chat.
The bot replies under that message with “Hugs given in this channel: 1”.
The next sequential run gives 2.
Use your channel’s prefix instead of ! if you changed it, and choose a name that is not already used by another command.
Follow one run through the bot
- A viewer types
!hugsin chat. The bot finds the command and sees that it runs a code script. The command runs the script directly, with no pipeline in between. - The bot keeps the id of the chat message, so a reply can answer it.
- The bot checks the script: it exists, it is switched on, the channel has script time left, and it has a version that passed its check.
- The bot checks the three grants of that version and allows only those.
- The script reads
hugs.countfrom the channel’s storage. - The script stores the new count under the same key.
- The script calls
nnz.api.chat.reply. The bot posts the line as a threaded reply to the viewer’s message, in the channel’s one bot voice. When the platform refuses the thread, the bot posts the line with a mention of the viewer instead. - The bot saves the time of the run on the script.
When the run does not end cleanly, the bot answers the viewer with one failure notice. By default it says “Sorry, that command hit a snag and didn’t finish.” When the script itself fails, the Scripts page shows Last run failed with the error.
When a call fails
These calls follow the rule in Script basics, “One rule for a bad value”.
A failed call gives back its empty result, and nnz.lastError says why.
The script goes on after a failed call.
A required value that is left out or null is not a failed call: it stops the whole run.
| What goes wrong | What happens |
|---|---|
| The store is full or the value is too long | storage.set gives false. The script replies with the reason instead of announcing a saved count. |
| The reply does not reach chat | A failed call with the code upstream_failed. The script goes on. |
| A value the bot cannot use, such as empty text | A failed call with the code invalid_argument. The script goes on. |
A required value that is left out or null | The whole run stops. The error names the call and the value, for example “chat.reply needs a message”. |