SuperOP535

OWOP Chatbot Base v0.2.2

Jun 18th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This script may give errors if your browser is old, if you need a good & updated browser use Firefox!
  2. // Changelog:
  3. // Changed in v0.2.2: No longer requires TM
  4. // Changed in v0.2.1: Added support for /tell'ed commands, added isTelled arg
  5. // ==UserScript==
  6. // @name         OWOP Chatbot Base
  7. // @version      0.2.2
  8. // @description  A base for making an OWOP chatbot
  9. // @author       SuperOP535
  10. // @match        *.ourworldofpixels.com/*
  11. // @match        *.wire.ddns.net:9001/*
  12. // @grant        none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16.     var bot = {
  17.         enabled: false, // change to true for auto-enable
  18.         prefix: '!'
  19.     };
  20.     const commands = {
  21.         // command name may not have spaces
  22.         // id will be the rank if there is no id, nick will be empty if /tell was used
  23.         help: (args, msg, nick, id, isTelled) => {
  24.             OWOP.chat.send('Commands: ' + Object.keys(commands).sort().join(', '));
  25.         },
  26.         'some-command': (args, msg, nick, id, isTelled) => {
  27.             OWOP.chat.send('Hello, person with ' + (isNaN(+id) ? 'rank' : 'id') + ' ' + id + (nick ? ' and nick ' + nick : ''));
  28.         }
  29.     };
  30.     function addBtn() {
  31.         if(!bot.enabled) {
  32.             const btn = document.createElement('button');
  33.             btn.innerHTML = 'Enable bot';
  34.             btn.onclick = function(){btn.style.display='none';bot.enabled=true;};
  35.             document.getElementById('chat').appendChild(btn);
  36.         }
  37.         OWOP.chat.recvModifier = (data) => {
  38.             if(typeof data != 'string' || !bot.enabled) return data;
  39.             const parsed = data.match(/(-> )?\[?\(?(\d+|.)\)?\]? ?>? ?(.*): \u200B?(.*)/);
  40.             if(!parsed) return;
  41.             const [tell, id, nick, msg] = parsed.slice(1);
  42.             const isTelled = tell == '-> ';
  43.             var args = msg.split(' ');
  44.             if(args[0].slice(0,1) != bot.prefix) return data;
  45.             const cmd = args.shift().slice(1);
  46.             if(commands.hasOwnProperty(cmd)) commands[cmd](args, msg, isTelled ? '' : nick, id, isTelled);
  47.             return data;
  48.         };
  49.     }
  50.     window.addEventListener('load', addBtn);
  51.     if(typeof OWOP != 'undefined') addBtn();
  52. })();
Add Comment
Please, Sign In to add comment