The big picture
It is three pieces. Two of them run on my PC, and the third is Twitch.
- TwitchYour chat message or channel point redemption starts here.
- The bridgeA small program on my PC. It reads chat, keeps track of coins, runs the votes and decides what is allowed.
- The game modIt lives inside the game. It does the actual thing to the run and reports back whether it worked.
The bridge never touches the game directly, and the mod knows nothing about Twitch. Each one has a single job, which makes it much easier to figure out which side broke when something does.
The game mods
Each game gets its own mod, because the two games are built completely differently.
Balatro
Balatro is written in Lua, so the mod is Lua too. It is loaded with Steamodded. The mod wraps the game's own update function, so it gets a turn every frame. From there it checks for new requests, waits for the right game state, and calls into the same game code a normal play would use.
Slay the Spire 2
Spire 2 runs on the Godot engine with C#, so this mod is C#. It uses the game's own mod loader, plus Harmony to hook into game methods for things like quest progress. Where it can, an effect goes through the game's own commands for gold, health, cards, powers and relics. That way a gifted relic behaves like a real relic.
The tricky part with Spire is that the game gets updated. When an update changes the code a mod relies on, the mod has to be fixed to match.
How they talk
The bridge and the mod talk through plain text files in a folder on my PC. It sounds primitive, but it is easy to debug: I can open the folder and read exactly what was said.
inboxholds requests from the bridge, one file each.outboxholds the mod's answers: done, waiting, or failed and why.state.txtis a snapshot of the run that the mod rewrites about once a second.
A request is just lines of key=value. A simplified one looks like this:
id=a1b2c3 effect=gold_rain user=some_viewer ttl=600
The classic problem with files is reading one while it is only half written. Requests are written under a temporary name and renamed once they are complete, so the mod only ever sees whole ones. The state file always ends with end=1. If that line is missing, the bridge ignores the snapshot and reads it again.
Timing and refunds
Games do not like being changed at random moments. You cannot add energy when there is no fight, or mess with a hand while cards are flying across the screen.
So every effect has a rule for when it is allowed to run. If this is not the moment, it waits in line. When it gets a legal moment, it fires and your name shows up on screen. If it waits too long, ten minutes by default, or the game says it cannot be done, you get your coins or channel points back.
There are also cooldowns and limits, so one very enthusiastic chat cannot fire everything at once. And I have a panic button, F8, that pauses all of it.
The Twitch side
The bridge is plain JavaScript running on Node, with no extra packages to install.
- Logging in: it uses Twitch's device login. I approve a code on Twitch's own site, so the bridge never sees my password.
- Listening: chat messages and redemptions arrive over Twitch EventSub, a live connection that my PC opens to Twitch. I do not need to host a public server for it.
- Rewards: the bridge creates the channel point rewards itself and can mark a redemption as done or refund it.
- The control panel: it is a web page that only my own PC can open.
The 50 reward problem
Twitch allows 50 custom rewards on a channel in total. Two games' worth does not fit, so starting one game's bridge swaps the other game's rewards out for its own. Closing the bridge window normally hides the rewards, so nobody spends points on a game that is not listening.
One word commands
The shop commands come straight from the item names. Squash Gold Rain into lowercase with no spaces and you get !goldrain. Each bot answers with the list for its own game.
Chat playing the game
Democracy Mode is the part I think is the coolest. The mod describes what is on the screen as a list of choices: the cards you can play, the paths on the map, the things in the shop. The bridge turns that list into a vote. When the timer ends, the winner goes back to the mod, and the mod makes that move in the game.
Then it looks at the new screen and does it again. A choice with only one option does not get a vote, because nobody wants to vote on a single button.
This website
The site is plain HTML, CSS and a bit of JavaScript, hosted on Vercel. The price lists are not typed out by hand. A build script reads the same item list each bot uses and turns it into the data these pages show. When I change an item, I rebuild and publish the site to match.
Each bridge has a couple hundred automated tests, and the site has its own check script. I can also run a bridge offline with pretend viewers voting, which is a lot better than finding a problem live on stream.
Fun bugs
These all really happened while building the Spire side.
The fight with nobody in it
The Assassinate effect killed the enemy perfectly. The fight just never ended, because the game only checks for a win when whatever did the killing asks it to. Now the mod asks after each effect.
The surprise feedback form
Quests were first picked with the F1 to F4 keys. It turns out the game already uses some of those, and F2 opens its feedback form. Quests are picked with the mouse now.
Paused forever
A "chaos paused" box stayed on screen with nothing paused. The game was still reading a status file from a bridge that had been closed days earlier. Old status files are ignored now.
Where did the rewards go?
I hid the rewards, closed the bridge, started it again, and they stayed hidden. Only the old run of the bridge remembered hiding them. Now the bridge shows them again when it starts.
Picture perfect text
Some descriptions in the game have little icons inside the sentence. The vote text got the picture's file path where the word Energy should have been.