KoalaBot Module API Reference


Installing Modules

Modules are written in javascript with the .js extension. To install, put the code into a text file, name it [modulename].js, and put it into the /modules/ folder. Restart KoalaBot.

Some example modules:

  • OBS Integration (skhmt) - Hosts a local website that you can open via OBS' CLR/Browsersource for follower, host, sub, and completely custom notifications. Also requires this.
  • Import Ankhbot Points (skhmt) - Imports your points database from Ankhbot to KoalaBot
  • Developer Options (skhmt) - Gives you the Chrome Developer Console, lets you see raw IRC messages, and gives you a button to temporarily turn off all commands.
  • Gambling (skhmt) - A gambling chatroom game.
  • How long _ has been following (skhmt) - An example of using jQuery's ajax functions to get data from an "external API" and return it when a user runs a command.

You can find more modules and get help in creating them at /r/koalabot.


apiAddCmd(keyword, functionName, rbac, desc)

Adds a command, makes it lower case. It will call the function name you send it. The function will be given these parameters: params (array), from (string), mod (boolean), subscriber (boolean)

Parameters

  • keyword (string) - The !command a user types in.
  • functionName (string) - The function to call.
  • rbac (string) - Role-based access control. Choose from: off, all, reg, sub, mod, or bot. Off disables the command, even for the streamer. All is self explanatory. Reg is for regulars and above (sub, mod, bot). Sub is for subscribers and above (mod, bot). Mod is for moderators and above (bot). Bot is for the bot itself AND the streamer.
  • desc (string) - A short description of the command.

Returns boolean - True if success, false if fail.

Example

  • apiAddCmd('test', 'commandTestThing', 'all', 'This is a test command!');
    function commandTestThing(params, from, mod, sub) {
    	// do something when the command is used, like...
    	apiSay( 'from: ' + from + ', params: ' + params.join(', ') );
    }
    

apiChangeRBAC(keyword, rbac)

Changes the access control of a command.

Parameters

  • keyword (string) - The !command a user types in.
  • rbac (string) - Role-based access control. See apiAddCmd.

Returns boolean - True if success, false if fail.


apiAddTab(moduleName)

Adds a module to the dropdown and creates a page. If the module only adds commands and doesn't require a user interface, this doesn't need to be used.

Parameters

  • moduleName (string) - The name of the module.

Returns boolean - The id of the page to $(id).prepend / $(id).html / $(id).append .

Example

  • // Making the tab
    var _myTab = apiAddTab('moduleTest');
    $(_myTab).html( `<div class="row-fluid">
    	<div class="col-sm-12">
    		Hello World
    	</div>
    </div>` );
    

apiSay(text)

Writes to the chat. It outputs as [+] to show it's a module rather than [!] that the base bot uses.

Parameters

  • text (string) - The text to say in the chat.

apiLog(text)

Only outputs to the chatlog and your chat window, but does not send a chat message for others to see. It is used to notify the streamer of things.

Parameters

  • text (string) - The text to log.

apiGetPath()

Gets the path to the mods folder, ex: C:\bot\mods\

Returns string - The path to the mods folder, including the trailing slash


apiGetChannelName()

Gets the channel name, which is also the streamer's username.

Returns string - The channel name.


apiGetBotName()

Gets the bot's username.

Returns string - The bot name.


apiGetPointsUnit()

Gets the unit name for points. For example, "gold" or "cats".

Returns string - The points unit.


apiGetPoints(username)

Gets the number of points a user has.

Parameters

  • username (string) - The username, case insensitive.

Returns integer - Null if not found, otherwise the amount of points of the user.


apiGetMinutes(username)

Gets the number of minutes a user has been in the stream while the bot is also in the stream.

Parameters

  • username (string) - The username, case insensitive.

Returns integer - Null if not found, otherwise the amount of minutes the user has been in the stream.


apiSetPoints(username, points)

Sets the points a user has. This is not for adding or subtracting points.

Parameters

  • username (string) - Case insensitive.
  • points (integer) - What to set the user's points to.

Returns integer - Null if not found, otherwise the new amount of points the user has.


apiModPoints(username, points)

Modifies the points a user has by a relative amount. Using this command with points at 5, for example, would add 5 points to the user's total.

Parameters

  • username (string) - Case insensitive.
  • points (integer) - What to add to or subtract from the user's point total. To subtract, use a negative number.

Returns boolean - Null if not found, otherwise the new amount of points the user has.

Example

  • // Adding 5 points to a user
    apiModPoints('skhmt', 5);
    
    // Subtracting 7 points from a user
    apiModPoints('skhmt', -7);
    

apiOpenFile(filename)

Opens a file in the \mods\ directory.

Parameters

  • filename (string) - Case sensitive, the path to the \mods\ directory is included.

Returns string - The file contents, null if it doesn't exist.

Example

  • // Loading a settings object
    var _mySettings = JSON.parse( apiOpenFile( 'modExampleSettings.ini' ) );
    
    // Loading a file with just text
    var _myText = apiOpenFile( 'modExampleOutput.txt' );
    

apiAppendFile(filename, text)

Appends a new line of text to the end a file in the \mods\ directory. If a file isn't found, it will automatically be created, then appended to.

Parameters

  • filename (string) - Case sensitive, the path to the \mods\ directory is included.
  • text (string) - What to add to the contents of the file.

Returns boolean - True if success, false if fail.


apiWriteFile(filename, text)

Writes a file in the \mods\ directory. This will completely over-write an existing file.

Parameters

  • filename (string) - Case sensitive, the path to the \mods\ directory is included.
  • text (string) - What to make the contents of the file.

Returns boolean - True if success, false if fail.

Example

  • // Saving a settings object
    apiWriteFile( 'modExampleSettings.ini', JSON.stringify( _mySettings ) );
    
    // Writing a file with a line of text
    apiWriteFile( 'modExampleOutput.txt', 'Hello World!' );
    

apiGetRecentEvents()

Gets an array of the recent events, in format:
[ { "time": (integer milliseconds since midnight of January 1, 1970), "type": (string), "text": (string) }, ... ]

Type will be SUB, HOST, FOLLOW, or anything that a module adds

Returns array - The most recent events in the above format.


apiAddRecentEvent(type, text)

Adds to the recent events array. Recent events is used to send to a page via ajax, most likely.

Parameters

  • type (string) - Can be anything, the bot uses SUB, HOST, and FOLLOW for those events. Use your own type if you need to.
  • text (string) - The data. For SUB, HOST, and FOLLOW, it's only the username.

apiHotKey(hotkey)

Adds a global hotkey. Supported keys: A-Z, 0-9, Comma, Period, Home, End, PageUp, PageDown, Insert, Delete, Arrow keys ( Up, Down, Left, Right ) and the Media Keys ( MediaNextTrack, MediaPlayPause, MediaPrevTrack, MediaStop )

Combine them with Ctrl, Alt, or Shift. Ex: Ctrl+Alt+Comma

On OSX, Ctrl is command. These global hotkeys will block the normal function of those keys.

Parameters

  • hotkey (string) - See above comments on format.

Returns object - Use to set functionality of the hotkey.

Example

  • var _ctrlAltComma = apiHotKey( 'Ctrl+Alt+Comma' );
    _ctrlAltComma.on( 'active', function() {
    	alert( 'CTRL + ALT + Comma has been pressed!' );
    } );
    

apiDB(filename)

Creates/loads a sqlite database. This is an object.

Parameters

  • filename (string) - The filename to open or create.

Functions

  • write() - Saves the database to disk. Returns true if success, false if an error.
  • sel(query) - Runs a SELECT query on the database. Returns an object in the format: { "array": [ ... ], "table": "<table> ... </table>" }
  • run(query) - Runs a CREATE TABLE, INSERT INTO, or DELETE FROM query. Returns true if success, false if fail.

Example

  • // Open or create the db moduleTestDB.sqlite
    var _db = apiDB( apiGetPath() + 'moduleTestDB.sqlite' );
    
    // Creates a table with columns: id, content
    _db.run( 'CREATE TABLE test_table (id INTEGER PRIMARY KEY, content TEXT);' );
    
    // Insert some test values...
    // Using NULL for the primary key automatically sets it as the next highest integer
    _db.run( 'INSERT INTO test_table VALUES (NULL, "hello world");' );
    _db.run( 'INSERT INTO test_table VALUES (NULL, "hello darkness my old friend");' );
    _db.run( 'INSERT INTO test_table VALUES (NULL, "hello moto");' );
    
    // Deleting 'hello moto'
    _db.run( 'DELETE FROM test_table WHERE content = "hello moto";' );
    
    // Outputting a HTML table
    $('#outputArea').html( _db.sel( 'SELECT * FROM test_table;' ).table );
    
    // Outputting the array
    $('#outputArea').append( JSON.stringify( _db.sel( 'SELECT * FROM test_table;' ).array ) );
    
    // Saving
    _db.write();