BonkBot is a JavaScript library for creating bots for the web game Bonk.io. It provides an easy way to create custom bots that can automate various actions within the game. With BonkBot, you can interact with the game's websocket protocol easily, chat with other players, and perform actions such as joining and leaving rooms.
To install BonkBot, you will need Node.js installed on your system. Then, run the following command in your terminal:
npm install bonkbot- Connect to existing rooms or create new ones
- Automatically determine the optimal server using the game's API
- Send and receive chat messages
- Track players joining and leaving
- Manage teams and game settings
- Handle game events
- Comprehensive error handling
- Detailed logging
const{ createBot,LOG_LEVELS}=require('bonkbot');// Create a bot instanceconstbot=createBot({account: {username: 'BotName',guest: true,// Use guest account},logLevel: LOG_LEVELS.WARN,// Set log level (DEBUG, INFO, WARN, ERROR, NONE)});// Initialize and handle eventsbot.events.on('ready',async()=>{awaitbot.connect();});// Handle chat messagesbot.events.on('CHAT_MESSAGE',(message)=>{console.log(`${message.player.username}: ${message.message}`);// Respond to !ping commandif(message.message==='!ping'){bot.chat('Pong!');}});bot.events.on('PACKET',(packet)=>{bot.autoHandlePacket(packet);});bot.init();By default, BonkBot will automatically determine the optimal server to connect to by querying the game's API. This ensures your bot connects to the most appropriate server based on the current game infrastructure.
If you want to override this behavior and connect to a specific server, you can provide the server option when creating the bot:
constbot=createBot({// ... other optionsserver: 'b2ny1',// Force connection to a specific server});constbot=createBot({account: {username: 'BotName',guest: true,},logLevel: LOG_LEVELS.WARN,});constbot=createBot({account: {username: 'YourUsername',password: 'YourPassword',guest: false,},logLevel: LOG_LEVELS.WARN,});The connection flow typically follows this sequence:
- Bot initialization (
bot.init()) - Ready event fires when login completes
- Connect to game server (
bot.connect()) - Find or create room
- Join room or create room
- Handle game events
bot.events.on('ready',async()=>{try{// Find room by nameconstroomInfo=awaitbot.getAddressFromRoomName('roomName');console.log(`Found room: ${roomInfo.roomname}`);// Set address and connectbot.setAddress(roomInfo);awaitbot.connect();// Join with optional passwordawaitbot.joinRoom({password: 'optional-password',});}catch(error){console.error('Failed to join room:',error);}});bot.events.on('ready',async()=>{try{// Get room info from share linkconstroomInfo=awaitbot.getAddressFromUrl('https://bonk.io/123abc');console.log(`Found room: ${roomInfo.roomname}`);// Connect to roombot.setAddress(roomInfo);awaitbot.connect();awaitbot.joinRoom();}catch(error){console.error('Failed to join room:',error);}});bot.events.on('ready',async()=>{try{// Connect to server firstawaitbot.connect();// Then create roombot.createRoom({roomname: 'BonkBot Room',maxplayers: 10,roompassword: '',hidden: true,});}catch(error){console.error('Failed to create room:',error);}});// Get share link when room is createdbot.events.on('ROOM_SHARE_LINK',(data)=>{console.log(`Room created! URL: ${data.url}`);});| Event | Description | Returns |
|---|---|---|
ready | Bot is ready to connect | - |
connect | Connected to server | - |
PACKET | Any packet received | {type, ...data} |
JOIN | Joined a room | {game, room, players} |
PLAYER_JOIN | Player joined room | {player, id} |
PLAYER_LEAVE | Player left room | {player, id} |
CHAT_MESSAGE | Chat message received | {player, message} |
TEAM_CHANGE | Player changed team | {player, team} |
HOST_TRANSFER | Host was transferred | {oldHost, newHost} |
READY_CHANGE | Player ready status changed | {player, ready} |
GAME_START | Game started | - |
GAME_END | Game ended | - |
COUNTDOWN | Game countdown | {countdown} |
MAP_SWITCH | Map was switched | {map} |
MAP_SUGGEST | Map was suggested | {title, author, player} |
CHANGE_ROUNDS | Round count changed | {rounds} |
GAMEMODE_CHANGE | Game mode changed | {mode, engine} |
ROOM_SHARE_LINK | Room link created | {url} |
PLAYER_KICK | Player was kicked | player |
PLAYER_TABBED | Player tabbed in/out | {player, tabbed} |
PLAYER_INPUT | Player input received | {player, movement} |
TEAMLOCK_TOGGLE | Teams locked/unlocked | {teamsLocked} |
ROOM_NAME_UPDATE | Room name changed | {name} |
ROOM_ADDRESS | Room address updated | {address} |
BALANCE_SET | Player balance changed | {player, balance} |
disconnect | Disconnected from server | - |
// Chat messagebot.chat('Hello world');// Get all playersconstplayers=bot.getAllPlayers();// Get hostconsthost=bot.getHost();// Get room share linkconstshareLink=bot.getShareLink();// Set player ready statusbot.ready(true);// Join a team (0-5)bot.joinTeam(2);// 0=spectator, 1=FFA, 2=red, 3=blue...// Give host to playerbot.giveHost(playerId);// Kick a playerbot.kickPlayer(playerId);// Leave the roombot.leaveGame();Check out the examples directory for more examples:
simple-bot.js: A basic bot that connects to a room and responds to chat commandshost-bot.js: A bot that creates and hosts a room
Contributions are always welcome! If you find a bug or have a feature request, please open an issue on the project's GitHub page.
BonkBot is open-source software licensed under the GPL-3.0 License.