Skip to content

Writing Custom Scripts

Erik Bigler edited this page Sep 15, 2017 · 50 revisions

Writing Custom Scripts

Firebot allows you to fire off custom scripts when a button is pressed. This enables users to extend Firebot's capabilities beyond the base app. As of v4.2, Firebot also supports callbacks that will allow custom scripts to trigger any effect that a button can do. Custom scripts must be written in JavaScript and are treated as a Node module, so at least a basic understanding of JavaScript will be required.

DISCLAIMER: USE THIS AT YOUR OWN RISK

Custom scripts from unknown sources can pose a risk to your system. Use with discretion.

Custom Scripts V2

As of v4.2, Firebot introduced an updated version of it's custom script feature. The original implementation of Custom Scripts V1 is still supported, so if you don't need to take advantage of the new features then you don't have to update anything. Custom Scripts V2 have the following new features:

  • Scripts can now define parameters that are shown to the user in the Edit Button Modal. The parameters are then passed back at the time of execution.
  • Scripts can now trigger any effect a button can, and can trigger multiple at once.
  • As of Firebot v4.2, the cached version of the script is no longer automatically purged on every button press (this allows things like persistent connections). However you can turn that feature back on in the settings, it's particularly useful while developing the script.

How does Firebot detect a V2 Custom Script?

A script is considered a V2 script if it's main run function only accepts 1 argument, instead of 3.

Simple script

There are only two requirements for a valid bare-bones V2 custom script:

  • a function that takes in one argument:
    • a runRequest object which will contain: buttonId, username, parameters, and modules
  • that function is exported under the name run so it's visible to Node (and therefore Firebot).

Example

function run(runRequest) {
    var buttonId = runRequest.buttonId;
    var username = runRequest.username;

    // Do some stuff
}

// Export 'run' function so it is visible to Node
exports.run = run;

Advanced script

If you want to callback to Firebot to run effects, you must return a Promise object and resolve with a response object. This will allow you to run asynchronous code without stalling Firebot.

Example

function run(runRequest) {
  // Return a Promise object
  return new Promise((resolve, reject) => {
    var buttonId = runRequest.buttonId;
    var username = runRequest.username;
    // Do some stuff

    // Create a response 
    var response = {
      success: true,
      errorMessage: "Failed to run the script!", // If the above 'success' is false, this message is shown as a popup in Firebot.
      effects: [] // An array of effect objects to run
    }

    // Resolve Promise with the response object
    resolve(response);

    // Note: We currently aren't acting upon the 'reject' call so don't be surprised when nothing happens if you try to use that.
  });
}

// Export 'run' function so it is visible to Node
exports.run = run;

Other Examples

Mixer API Example

Here is an example of getting information from the Mixer API about a user who pushed the button, and then broadcasting that to chat. In this case we get a user's level, sparks, and experience. Note that I am using the 'request' module. This is accessible via the modules argument. Currently, Firebot only passes over the 'request' package at this time.

function run(runRequest) {
  var buttonId = runRequest.buttonId;
  var username = runRequest.username;

  const request = runRequest.modules.request;

  // Return a Promise object
  return new Promise((resolve, reject) => {
    var url = 'https://mixer.com/api/v1/channels/'+username;
      
	request(url, function (error, response, data) {

          var response = {};

	  if (!error) {
            // Got response from Mixer.
            var data = JSON.parse(data);

            // Find the data we want in the Mixer json.
            var level = data['user'].level;
            var sparks = data['user'].sparks;
            var exp = data['user'].experience;

            // Build our message.
            var message = username+" || Level: "+level+" || Sparks: "+sparks+" || Exp: "+exp;

            // Create a success response 
            response = {
                success: true,
                effects:[
                    {
                        type: EffectType.CHAT,
                        message: message,
                        chatter: "streamer"
                    }
                ]                  
            }
	  } else {
            // We had an error with the mixer request. So, create an error popup in Firebot.

            // Create a failed response 
            response = {
              success: false,
              errorMessage: 'There was an error retrieving data from the Mixer API.'                         
            }
         }
         // Resolve Promise with the response object
         resolve(response);
     })
  });
}

// Export 'run' function so it is visible to Node
exports.run = run;

Scripts by the Community

ThePerry: I'm currently working on making a custom script for 7 Days to Die: https://gist.github.com/ThePerry-2015/ad60287370400cd810441964b79b0289

It's not adjusted for A16 yet since it relies on Valmod Expansion Pack, and Valmod has had some RL stuff to take care of so he hasn't gotten around to update yet. I'll update it for A16 as soon as Valmod Expansion Pack is released.

Clone this wiki locally