AI replies uh do /key (key) to set openrouter api key do /ai to toggle on/off its saved in localStorage
Bonk.io AI Auto-Reply Script - Step-by-Step Guide
This userscript for Bonk.io uses the OpenRouter API to automatically generate and send AI replies in the chat. Below is a detailed step-by-step breakdown of how it works and how you can use it.
1. Setup and Prerequisites
To use this script, you need the following:
Tampermonkey or Greasemonkey browser extension to run userscripts.
An OpenRouter API key. You can sign up for a free API key at OpenRouter
.
2. Features and Commands
The script includes several core features and commands for controlling the AI and interacting with the chat:
/ai:
Enable or disable AI replies.
By default, the AI is off. When enabled, it will automatically reply to chat messages.
/key YOUR_OPENROUTER_KEY:
Set your OpenRouter API key to allow the script to communicate with the OpenRouter API and get AI responses.
/cmds:
List all available commands for easy reference.
3. Script Workflow
A. Chat Message Detection
The script continuously monitors the Bonk.io chat for new messages.
When a new message is added to the chat, the script checks whether the AI is enabled and whether the message passes certain filters (e.g., ignoring system messages like "Rate limit").
If the AI is enabled and the message is valid, it will be sent to the OpenRouter API for a response.
B. Sending a Message to the AI
When a message is detected and the AI is enabled, the script sends the message to the OpenRouter API using an HTTP POST request.
The request contains:
The AI model to use (mistralai/devstral-2512:free).
The user's chat message as input to the AI.
Additional parameters to control the output (short replies, temperature settings).
C. AI Response Handling
Once the AI responds, the script captures the response.
The response is then sent back to Bonk.io's chat as an AI-generated reply.
The script simulates the action of typing the message in the chat box, then submitting it automatically.
D. Cooldown and Self-Ignore
To prevent spamming the chat with replies, the script includes a cooldown timer (COOLDOWN_MS = 3000), which ensures replies are spaced out by at least 3 seconds.
If the AI replies too quickly, it waits a few moments before responding again.
4. Detailed Code Breakdown
A. DOM Elements
These are the elements that interact with the chat:
Lobby Input (lobbyInput): The input box where players type their messages.
Lobby Content (lobbyContent): The chat container where all messages are displayed.
Game Input (gameInput): The in-game chat input box.
Game Content (gameContent): The in-game chat message container.
B. OpenRouter Configuration
ENDPOINT: The URL of the OpenRouter API (https://openrouter.ai/api/v1/chat/completions).
MODEL: Specifies which AI model to use. In this case, it's mistralai/devstral-2512:free (a free model from OpenRouter).
C. State Management
apiKey: Stores the OpenRouter API key (retrieved from localStorage).
enabled: Stores whether the AI feature is enabled or disabled (also from localStorage).
inFlight: A flag that prevents multiple AI requests from being sent at the same time (prevents race conditions).
ignoreUntil: Helps with cooldown management to avoid too-quick replies.
D. Command Handler
The script listens for commands typed into the chat. Commands are identified by the / prefix.
/ai toggles the AI feature on or off.
/key saves the OpenRouter API key.
/cmds lists the available commands.
E. Mutation Observer
A MutationObserver is used to detect new messages in the chat. When a new message appears:
If the AI is enabled, it sends the message to the AI for a response.
Filters are applied to ignore certain messages (like system messages).
F. OpenRouter Request
The AI is queried via a POST request, passing the chat message as input.
The response from the OpenRouter API is captured, and the AI's reply is sent back to the chat.
G. Sending Chat Messages
After receiving the AI's reply, the script simulates a keyboard event (Enter key) to "send" the AI's reply in the chat.
This is done twice to ensure the message is successfully posted and the input box is cleared.
5. How to Install and Use the Script
Step 1: Install the Userscript
Install Tampermonkey or Greasemonkey extension in your browser.
Create a new script and paste the entire code into it.
Save the script.
Step 2: Set the OpenRouter API Key
After installation, visit Bonk.io and open the game/chat.
In the chat, type /key YOUR_OPENROUTER_KEY, replacing YOUR_OPENROUTER_KEY with the key you received from OpenRouter.
Step 3: Enable AI Replies
Type /ai in the chat to enable the AI. The AI will start responding to new chat messages automatically.
Step 4: Test and Use Commands
Type /cmds to see a list of available commands.
Enjoy automatic AI replies in the chat!
6. Troubleshooting and Tips
No AI replies? Make sure your OpenRouter API key is set correctly using the /key command and that the AI is enabled with /ai.
Rate limit errors? The script automatically ignores system messages about rate limits, but if you hit the limit too frequently, try waiting a bit before enabling the AI again.
Spammed replies? Adjust the cooldown time by modifying the COOLDOWN_MS variable in the script.