# Ticket.bot

Prints a little thermal **ticket** every time someone supports your stream: cheers, subs, gift subs, raids, **Twitch Power-Ups** (built-in and custom), YouTube Super Chats and members, Kick subs and Kicks, tips (StreamElements, Streamlabs, Ko-fi, TipeeeStream) and Fourthwall orders.

Built around a **safety throttle**. When chat spams Power-Ups, Ticket.bot doesn't fire 100+ jobs at your printer. It prints **one ticket at a time** with a cooldown, gives the printer **rest breaks**, has an **hourly limit**, and **merges** spam into one "×12" ticket. Nothing is lost, and there's a big **EMERGENCY STOP** button.

```
dock/                    → the OBS dock (published at https://widgets.nonamefound.lol/ticket-bot by the nnf-widgets project)
  index.html, dock.css, app.js
  ticket.js              ticket model → printable HTML
  renderers.js           Streamer.bot event → ticket (one renderer per event type)
  queue.js               queue, pacing, merging (the dock's safety layer)
  samples.js             sample events for the Test buttons and the tests
ticket/
  ticket.css             the printed ticket design (black & white, 80 mm)
  preview.html           every sample ticket side by side, for working on the design
assets/                  logo (colour for the dock, black & white for the paper)
streamerbot/
  TicketBot.import.txt   paste into Streamer.bot › Import
  Events.cs, Print.cs, Control.cs   the C# code of each action (same as in the import)
  build-import.js        rebuilds the import after editing the .cs files (node streamerbot/build-import.js)
print-tools/             where wkhtmltopdf + SumatraPDF come from (see print-tools/README.md)
tests/                   tests that run without Streamer.bot and without a printer (see "Testing")
```

## How it works

```
Streamer.bot event ──► Ticket.bot | Events ──websocket──► OBS dock
                                                          │ builds the ticket, queues it,
                                                          │ merges spam, waits between tickets
                         Ticket.bot | Print  ◄──one at a time──┘
                         (blocking queue "Ticket.bot")
                         hard limits → HTML → PDF (wkhtmltopdf) → printer (SumatraPDF)
                         waits until the Windows print queue is empty, then answers the dock
```

The safety rules exist **twice**: in the dock and again in Streamer.bot (`Print.cs`). Streamer.bot's limits hold even if the dock misbehaves, two docks are open, or something calls the print action in a loop:

| Rule | Default (dock) | Hard limit in Streamer.bot |
|---|---|---|
| Gap between tickets | 8 s after the previous one finished | never below 4 s |
| Rest | 90 s after 20 tickets in a row | at least 30 s, at most every 30 tickets |
| Tickets per hour | 150 | never above 300 |
| Jobs at the same time | 1 | 1 (blocking queue + waits for the Windows print queue to empty) |
| Printer stuck (paper out, jam, offline) | – | after 120 s everything stops until you release it |
| Same ticket twice | ignored | rejected (de-duplicated by ticket id) |
| Merging | when 10+ tickets wait, same person + same thing → one "×N" ticket | – |

Example: the 150 Power-Ups that set a printer on fire at 35 tickets/minute become about **46 tickets at ≤ 5/minute**, with rest breaks (see `tests/queue-sim.js`).

## Install

### 1. Files in Streamer.bot
1. Create the folder `<Streamer.bot>\nnf-sb-plugins\tickets-bot\print-tools\`.
2. Put `wkhtmltopdf.exe`, `wkhtmltox.dll` and `sumatrapdf.exe` in it (see [print-tools/README.md](print-tools/README.md)).
   The `temp`, `output`, `state` and `debug` folders are created automatically.

### 2. Streamer.bot
1. **Servers/Clients → WebSocket Server**: enable it (default `127.0.0.1:8080`) and tick *Auto Start*.
2. **Import** → paste the contents of `streamerbot/TicketBot.import.txt` → Import. This adds the group **Ticket.bot** with 3 actions and a **blocking** queue named **Ticket.bot**:

| Action | Triggers / use |
|---|---|
| **Ticket.bot \| Events** | All supporter events (already set). Remove the ones you don't want, or switch them off in the dock. |
| **Ticket.bot \| Print** | No trigger. Called by the dock. **Must stay in the "Ticket.bot" queue.** |
| **Ticket.bot \| Control** | No trigger. Optional: bind it to a hotkey, a Stream Deck button or a mod-only chat command like `!ticket` (`!ticket stop`, `!ticket resume`). |

3. Open each action's *Execute C# Code* sub-action and click **Compile** to check it says *Compiled successfully*.
4. Check the triggers of **Ticket.bot | Events**:
   - *Custom Power-Up Redemption* is set to **Any**. If you only want some custom Power-Ups, replace it with one trigger per Power-Up.
   - *Power-Up Redemption* covers the 3 built-in Twitch Power-Ups (On-Screen Celebration, Gigantify an Emote, Message Effects).
   - Ko-fi and TipeeeStream tickets are supported, but their triggers are not in the import. Add them by hand if you use them.

> **Coming from Nutty's Printer Bot?** Disable (or delete) *Printer Bot | Events* and *Printer Bot | Print Routine*, and remove the old Printer Bot dock from OBS. Otherwise both will print.

### 3. OBS dock
**Docks → Custom Browser Docks…** → name `Ticket.bot`, URL:

```
https://widgets.nonamefound.lol/ticket-bot
```

If your Streamer.bot WebSocket uses another port, or a password, add it to the URL: `https://widgets.nonamefound.lol/ticket-bot?port=8080&password=YOURPASSWORD` (`host=` is also supported).

The dock must be open for tickets to be created (like the original Printer Bot). If OBS is closed, the queue is kept and continues next time.

### 4. First run (no printer needed)
The dock starts in **PDF test mode**: tickets are written to `<Streamer.bot>\nnf-sb-plugins\tickets-bot\output\` instead of being printed.

1. **Test** tab → *Print 1 test ticket* → a PDF appears in `output\`.
2. *One of each event* → one PDF per event type.
3. *Spam test: 150 Power-Ups* → watch the queue merge and pace the tickets.

When you have a printer: **Settings** → Output **Printer** → type the printer name exactly as in *Windows › Settings › Printers & scanners*.

## Using it

- **EMERGENCY STOP** (dock, or `Ticket.bot | Control` with `cmd = stop`):
  - stops printing at once,
  - kills a running SumatraPDF from `print-tools`,
  - **cancels the printer's jobs in the Windows print queue**.

  The stop is saved: it survives a Streamer.bot restart until you press **Release stop** (or run Control with `cmd = resume`).
- **Pause** only pauses the dock's queue; tickets keep collecting.
- **Queue** tab: what's waiting (✕ removes one). **History**: the last 30 tickets with their number.
- **Settings**:
  - output and printer,
  - paper width,
  - *Trim paper to ticket length*, which cuts the page to the ticket instead of a 500 mm page,
  - the safety limits,
  - which events print,
  - the text on the ticket.
- Every printed ticket is logged in `state\print-log.csv`.
- Custom triggers for your own actions: **Ticket Printed** (`ticketNo`, `ticketSource`, `ticketUser`) and **Printer Halted** (`haltReason`), under *Custom › Ticket.bot*.

## Troubleshooting

- **"Printer busy/stuck before printing"**: there are old jobs in the Windows queue of that printer. Open *Printers & scanners › your printer › Open print queue* and cancel them, or run Control with `cmd = purge`. Then **Release stop**.
- **Nothing happens**:
  - check the dock says *Connected*,
  - check that the actions are imported and compiled,
  - check that **Ticket.bot | Print** is in the **Ticket.bot** queue.
- **"wkhtmltopdf.exe … not found"**: the print tools are not in `nnf-sb-plugins\tickets-bot\print-tools\`.
- **A field is missing on a ticket** (e.g. a new Power-Up): every event's arguments are saved in `nnf-sb-plugins\tickets-bot\debug\<EventType>.json`. Compare them with the renderer in `dock/renderers.js`. Turn this off with the global variable `ticketBot_debugDump = false`.

## Testing (developers)

Nothing here needs Streamer.bot running or a printer.

| Command | What it checks |
|---|---|
| `node tests/queue-sim.js` | 150-redemption spam through the dock queue with a fake clock: one at a time, gaps, rests, merging, nothing lost |
| `node tests/run-harness.js --sb "<Streamer.bot folder>"` | Compiles the real `Events.cs`/`Print.cs`/`Control.cs` against a fake CPH (needs VS Build Tools' Roslyn `csc.exe`). Checks the hard limits, dedupe and emergency stop, and renders every sample to a real PDF in `tests/output/pdf` |
| serve the folder (`python -m http.server`) and open `/tests/dock-e2e.html` | The real dock against a mock Streamer.bot: merging, pacing, page height, dedupe, emergency stop, persistence |
| open `/ticket/preview.html` | Every ticket design at once |

After editing a `.cs` file, run `node streamerbot/build-import.js` to rebuild the import (action ids stay the same, so re-importing replaces the old actions).

## Credits

Inspired by [Printer Bot by Nutty](https://nuttylmao.notion.site/Printer-Bot-25c19969b23780e6b861ebaeda8568b4), which showed how to print from Streamer.bot with wkhtmltopdf + SumatraPDF. Ticket.bot is a rewrite with its own dock, design and safety system.
