Advertisement
Guest User

bot

a guest
May 19th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Message {
  2.   constructor(user, txt, starNode, replyID) {
  3.     this.replyID = replyID;
  4.     this.user = user;
  5.     this.txt = txt;
  6.     this.starNode = starNode;
  7.     console.log("Message created: " + this.txt);
  8.   }
  9.   star() {
  10.     this.starNode.click();
  11.     console.log("Message starred: " + this.txt);
  12.   }
  13.   toMarkdown() {
  14.    
  15.   }
  16.  
  17. }
  18. /* Copyright(c) 2016 Caleb Gentry alias epicTCK
  19.  * Permission is hereby granted, free of charge,
  20.  * to any person obtaining a copy  of this software
  21.  * and associated documentation files(the "Software"),
  22.  * to deal in the Software  without restriction,
  23.  * including without limitation the rights to use,
  24.  * copy, modify, merge, publish,  distribute, sublicense,
  25.  * and / or sell copies of the Software, and to permit
  26.  * persons to whom the Software is furnished to do so,
  27.  * subject to the following conditions:
  28.  *
  29.  * The above copyright notice and this permission notice shall be included in
  30.  * all copies or substantial portions of the Software.
  31.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  34.  * IN NO EVENT SHALL THE AUTHORS  OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  35.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  36.  * TORT OR OTHERWISE, ARISING FROM, OUT  OF OR IN CONNECTION WITH THE
  37.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  38.  */
  39. var loopControl = () => setInterval(bot.loop, 1000);
  40. class Bot {
  41.   constructor(name, owner, ignoreOwn, reqPing) {
  42.     this.name = name;
  43.     this.owner = owner;
  44.     this.reqPing = reqPing;
  45.     this.ignoreOwn = ignoreOwn;
  46.     this.modules = new Set();
  47.     console.log("Bot created.");
  48.     setInterval(this.loop, 1000);
  49.   }
  50.   chat(msg) {
  51.     document.getElementById('input').value = msg.replace(/<\/?i>/g, "*");
  52.     document.getElementById("sayit-button").click();
  53.     console.log("message sent: " + msg);
  54.   }
  55.  
  56.   getMessage(number) {
  57.       var containers = document.getElementsByClassName("monologue");
  58.       var container = containers[containers.length - number];
  59.       var message = new Message(
  60.         container.getElementsByClassName("username")[0].innerHTML,
  61.         container.getElementsByClassName("content")[0].innerHTML,
  62.         container.getElementsByClassName("stars")[0].children[0],
  63.         ":" + container.getElementsByClassName("message")[0].id.split("-").pop()
  64.       );
  65.       console.log("Message returned: " + message.txt);
  66.       return message;
  67.     }
  68.  
  69.   loop() {
  70.     if(this.run){
  71.       var message = this.getMessage(1);
  72.       var prevMessage = this.getMessage(2);
  73.  
  74.       if (message.user == this.name && this.ignoreOwn) {
  75.         return;
  76.       }
  77.       var messageL = message.txt.toLowerCase();
  78.  
  79.       if (this.reqPing && messageL.includes("@" +
  80.           this.name.toLowerCase()) != true)
  81.         return;
  82.       if (message.txt == prevMessage.txt)
  83.         return;
  84.       for (let module of this.modules) module(this, message);
  85.     }
  86.   }
  87.  
  88.   stop() {
  89.    
  90.   }
  91.   start() {
  92.     loopControl();
  93.   }
  94.   addModule(callback) {
  95.     this.modules.add(callback);
  96.     console.log("Module added: " + callback);
  97.   }
  98.  
  99.  
  100. }
  101.  
  102. function xkcd(bot, input) {
  103.  
  104.   if (input.txt.includes("xkcd")) {
  105.     var split = input.txt.split(" ");
  106.     var num = split[split.indexOf("xkcd") + 1];
  107.     var url = "http://www.xkcd.com/" + num;
  108.     if (url != "http://www.xkcd.com/NaN") {
  109.       bot.chat(url);
  110.     } else {
  111.       bot.chat("Invalid input " + input.replyID); //thnx to downgoat on PPCG chat
  112.     }
  113.   }
  114. }
  115. var bot = new Bot("epicTCK", "epicTCK", false, true);
  116. bot.addModule(xkcd);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement