MultiSnake Developer Documentation

First steps

Head over to the developer page to grab you API keys. Hit “Add key”, then navigate your favorite code editor.
The wrapper class for managing bots is only available in nodejs as of the time of this writing. If you want to create one without the
First, install the module using the following code

npm install multisnake-bot-api

Then, import it into your code.

const { BotManager} = require("multisnake-bot-api");

Player Bot

You can then create a new bot using the BotManager class.

let myBot = new BotManager({
	name: "My Super Cool Bot",
	rooms: ["classic-classic_0","classic-classic_1","standard-standard_0"],
	log: false,
	botOpts: {
		api_key: "<your api key from the developer page>", // remember to keep this private
		uid: "<your bot's uid from the developer page>"
	},
	onNeedDirection: handleDirection
});

First, we give the bot a name, in this case My Super Cool Bot (it can be anything), then we define what rooms we want to allow it to join. In this case, 3 rooms, two classic room and one standard room. The room names are the same as the name on the play screen, (classic_0, small_1, etc), except the name of the mod is added before. Eg. classic-classic_0, etc.
Then we specify whether we want to log things to the console or not. This will log when the bot is connected, and other things like that.
Next, you specify your credentials that we acquired from the developer page. Paste them into their respective spots, but make sure to keep the api_key secret (use environment variables- if you are doing this on replit, click on the secrets tab)

Finally, we need to specify how the bot move. We do this using the handleDirection function specified earlier.
It takes two parameters, board and room, board being an object representing the room, and room being the name of the room.
For more information, see the full documentation.
For now, we will just create a simple bot that goes towards the apple.

function handleDirection(board,room){
	let apple = board.apple;
	let snake = board.snakes.find(snake => snake.uid == uid);
	let head= snake.body[0];
	let dirToGo = "";
	if(head[0] > apple[0]){
      dirToGo = "left"
    }else if(head[0] < apple[0]){
      dirToGo = "right";
    }else if(head[1] > apple[1]){
      dirToGo = "down";
    }else if(head[1] < apple[1]){
      dirToGo = "up";
    }
	return dirToGo
}

This bot is very bare bones, and will turn onto itself, not avoid other snakes, and run into walls, but, its a start. For further reading on the board object, see the room docs.

For the full code, check out https://replit.com/@sojs/MultiSnake-API-test#index.js.

Chat bot

Getting Started With Chatbots

Example

Let us pick apart this example.

const { BotManager } = require("multisnake-bot-api"); 
const delim = "/"
// @param opts
//    options
//      "name" - the name of the bot
//      "api_key" - your API key, retrieved from https://multisnake.xyz/developers (keep this private)
//      "uid" - your bot's UID, also from the developer page. This is public
const bot = new BotManager({
  "name": "MyAwesomeBot",
  "api_key":process.env.APIKEY,
  "uid":process.env.UID
});

// @param command - the command that the user sent
// @return true or false wether the command should be passed to onCommand()
bot.isCommand = (command) => {
  // Returns true or false whether or not the given param is a command
  return command.startsWith(delim);
}


// @param username - the username of the persone who sent the message
// @param message - the content of the message
// @param sendMessage - function to run in order to send a response
//    @param message - the content of the message to sent
// @param room - UID/name of the room
bot.onCommand = async (username, message, sendMessage, room) => {
  let command = message.split(" ")[0];
  command = command.slice(0,delim.length);
  let args = message.split(" ");
  args.shift();
  switch (command){
    case "room":
      sendMessage(`Name of this room is ${room}`);
      break;
    case "hi":
      sendMessage(`Hello, ${username}!`);
      break;
    case "echo":
      sendMessage(args.join(" "));
      break;
  }
}

To make a chat bot, we need to overwrite the isCommand method of the bot manager.

bot.isCommand = (command) => {

The isCommand method is called with one parameter, command, which is a string. If the function returns true, then the bot command will count this as a command.

This version of isCommand will only count something as a command if they start with “/”

bot.isCommand = (command) => {
  return command.startsWith("/");
}

Okay, there’s quite a bit to unpack in the example below this one. The first parameter, username, is a string that is the username of the person who sent the chat message. Anyone can have any username, so be careful not to use it as a foolproof way to identify someone. message, the text content of the message. sendMessage is a function that allows you to send a reply to this command. It will send a message in the room that this command was executed from. room contains the name of the room the command originated from.

bot.onCommand = async (username, message, sendMessage, room) => {

Then, we grab the command part of the message using split. The command will contain your delimiter, so you have to make sure to chop it off as well. We can do this pretty well using the following two lines.

let command = message.split(" ")[0];
command = command.slice(0,delim.length);

The rest of the message we will store in args for future use, in case any of your commands take parameters.

let args = message.split(" ");
args.shift();

args.shift() ensures that the actual command part of the message is left out, and we are left with just the arguments.
Finally, we can run a simple switch statement to figure out what response we want to send.

switch (command){
  case "room":
    sendMessage(`Name of this room is ${room}`);
    break;
  case "hi":
    sendMessage(`Hello, ${username}!`);
    break;
  case "echo":
    sendMessage(args.join(" "));
    break;
}

Notice the usage of args in the last example. This will ensure that when /echo is ran, the bot will repeat back what the user sent after echo. For example, if the user sends /echo hello world, the bot will take off the first bit ,/echo, and compare that to each case in your switch statement. Then, we join args back together (args being ["hello","world"]) with a space and sent it right back.

The very last thing is optional. We can set a default case in the event that the bot does not know how to respond to something.

switch (command){
    ...
  default:
    sendMessage(`I could not understand ${command}. I can understand "/room", "/hi", and "/echo"`);
    break;
}