Skip to content

Instantly share code, notes, and snippets.

@epicTCK
Last active March 27, 2016 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epicTCK/70f4e4fa7ee8eb292e7a to your computer and use it in GitHub Desktop.
Save epicTCK/70f4e4fa7ee8eb292e7a to your computer and use it in GitHub Desktop.
/* Copyright(c) 2016 Caleb Gentry alias epicTCK
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software
* and associated documentation files(the "Software"),
* to deal in the Software without restriction,
* including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense,
* and / or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function loopFix(){
bot.loop(bot);
}
class Message{
constructor(user, txt, starNode, replyID){
this.replyID = replyID;
this.user = user;
this.txt = txt;
this.starNode = starNode;
}
star(){
this.starNode.click();
}
toMarkdown(){
}
}
class Bot {
constructor(name, owner, ignoreOwn, reqPing) {
this.name = name;
this.owner = owner;
this.reqPing = reqPing;
this.ignoreOwn = ignoreOwn;
this.modules = new Set();
}
chat(msg) {
document.getElementById('input').value = msg.replace(/<\/?i>/g, "*");
document.getElementById("sayit-button").click();
}
getMessage(number) {
var containers = document.getElementsByClassName("monologue");
var container = containers[containers.length - number];
if(container.classList.contains("mine"))
return "MINE";
var message = new Message(
container.getElementsByClassName("username")[0].innerHTML,
container.getElementsByClassName("content")[0].innerHTML,
container.getElementsByClassName("stars")[0].children[0],
":" + container.getElementsByClassName("message")[0].id.split("-").pop()
);
return message;
}
/* The fix for loop is ugly. Just add a sleep at the end instead of using
* A setInterval and then check for a bool to end/ begin the loop
* using bot.start() and bot.stop()
*
*/
loop(thisFix) {
var message = thisFix.getMessage(1);
var prevMessage = thisFix.getMessage(2);
if(message === "MINE"){return;}
var prevMessageTxt = prevMessage.txt == "MINE"? "MINE": prevMessage.txt;
var messageL = message.txt.toLowerCase();
do {
if (thisFix.reqPing && messageL.includes("@" +
thisFix.name.toLowerCase()) != true)
break;
if (message.txt == prevMessageTxt)
break;
for (let module of thisFix.modules) module(thisFix, message);
} while (false);
}
stop() {
clearInterval(this.loopInterval);
}
addModule(callback) {
this.modules.add(callback);
}
start(interval) {
this.loopInterval = setInterval(loopFix, interval);
}
}
function xkcd(bot, input) {
if(input === "MINE"){return;}
if (input.txt.includes("xkcd")) {
var split = input.txt.split(" ");
var num = split[split.indexOf("xkcd") + 1];
if(/\s+xkcd\s+\d+\s*$/.test(input.txt)){
bot.chat("http://www.xkcd.com/" + num);
}else{
bot.chat("Invalid input " + input.replyID);//thnx to downgoat on PPCG chat
}
}
}
var bot = new Bot("epictck", "epictck", true, true);
bot.addModule(xkcd);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment