Skip to content

Examples

Craig Edwards edited this page Nov 15, 2019 · 12 revisions

Here are some example scripts for use in channels. Feel free to directly paste these into the editor and play with them to see how they work.

Remember that you don't need to copy and paste scripts into sporks, many scripts are available on codebottle.io and can be downloaded directly into the bot with no programming knowledge required.

A simple swearword detector

if (message.content.match(/shit/i)) {
    create_message(CHANNEL_ID, "@moderators " + author.username + " is swearing :(");
}

A calculator script

Responds to !calc and simple math expressions

var arr = message.content.split(' ', 2);
if (arr[0] == '!calc') {
		if (arr[1].match(/^([-+/*]\d+(\.\d+)?)*/)) {
			create_message(CHANNEL_ID, arr[1] + " = " + eval(arr[1]));
        }
}

A karma script

Responds to !karma , watches for ++ and -- to adjust the karma score of that word.

var arr = message.content.split(' ');
if (arr[0] == '!karma') {
  var k = load("karma_" + arr[1]);
  if (k == undefined || parseInt(k) == 0) {
    create_message(CHANNEL_ID, arr[1] + " has neutral karma.");
  } else {
  	create_message(CHANNEL_ID, "karma for " + arr[1] + " is " + k);
  }
} else {
  for (i = 0; i < arr.length; ++i) {
    var modifier = 0;
    if (arr[i].slice(-2) == "--") {
      modifier = -1;
    } else if (arr[i].slice(-2) == "++") {
      modifier = +1;
    }
    if (modifier != 0) {
      var word = arr[i].substring(0, arr[i].length - 2);
      var k = load("karma_" + word);
      if (k == undefined) {
        k = modifier;
      } else {
        k = parseInt(k);
        k += modifier;
      }
      save("karma_" + word, k.toString());
    }
  }
}