Skip to content

Bot API Tutorial (part 2): The basics.

_iPhoenix_ edited this page Jan 27, 2018 · 8 revisions

UniChat has a bunch of bots, and they are really easy to create.

Here are the variables:

  • Message-specific variables.
    • These are variables that are changed by the API for each message. They are read-only, but you can create copies of them.
    • poster: The username of the user who posted the message.
    • message: The message itself. This does not include the timestamp, header char, or username. The message is converted to lowercase, because commands are supposed to be case-insensitive.
    • timestamp: Contains a formatted version of the time, like 12:30:45.
    • timestamp_raw: Contains a raw version of the timestamp, like 1513877445385.
    • rawMessage: The message exactly as it was typed by the user. This includes the header char and is not made lowercase.
  • Bot-specific variables.
    • These are variables that determine how your bot functions.
    • bot_username: The name of your bot.
    • bot_header_char: The character that determines whether your bot is called on a specific message or not. For iPhoenixBot and karmaBot, this is ~.
  • Universal constants.
    • These are constants that cannot be changed and are used in functions.
      • messageStatus.error: Used with log(), this signifies that something has gone wrong.
      • messageStatus.warning: Used with log(), this signifies that something might go wrong.
      • messageStatus.message: Used with log(), this signifies that everything is fine.

Here are the (main) functions:

  • All JavaScript functions.
  • var bot = new Bot(bot_name, bot_command_header[, version]): This function creates a bot, bot (of course, you should change this to whatever bot_name is, for clarity), that you can do things with. You can optionally add a version number to your bot by passing it an additional argument.
  • bot.register(): This registers your bot (created with the previous command) so that it works with UniChat.
  • bot.respond(message): This is how you will interact with the users! This function takes your response to your command and sends it to the user.
  • bot.executeCommand = function(data) {}: This is the main part of your bot. This is where command data is sent to be parsed by your scripts.
  • bot.userJoined = function(data) {}: This is called when a user joins the room.
  • bot.userLeft = function(data) {}: This is called when a user leaves the room.
  • bot.defineSimpleCommand(command, response): This defines a simple command. Whenever command is sent, your bot responds with response.
  • initializeBots(): This function is called when the API is ready for the bots to be loaded. If you have multiple bots, then they should share the same initializeBots() function. This is where you put your bot.register() function calls.
  • bot.activate(): Makes an inactive bot active.
  • bot.deactivate(): Makes a bot that is active inactive and not able to accept commands.
  • bot.version: This is the version number of your bot that you set when you created the bot. If no version was specified, this defaults to "v1".
  • log(messageStatus, message): Logs data to the botAPI console. The input messageStatus is one of the three constants, messageStatus.error, messageStatus.warning, or messageStatus.message. The message is whatever you want to log to the console.

Be sure to check out Part 3: Creating your first bot.