InsultBot

Say @insult

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         InsultBot
// @namespace    http://apvark.netai.net/
// @version      0.9
// @description  Say @insult
// @author       Supervarken + original code by AlexCatch
// @match        https://www.reddit.com/robin
// @include      https://www.reddit.com/robin/
// @grant GM_xmlhttpRequest
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==

(function() {
   'use strict';
    var websocket;
    var wsUri = r.config.robin_websocket_url;

    websocket = new WebSocket(wsUri);
    websocket.onmessage = function(evt) {onMessage(evt); };
    
    setTimeout(function() {
        sendMessage("I am a bot, send me a message with either !insult, @insult or @insults for an insult!");
    }, 2000);
    
    setInterval(function(){ sendMessage("I am a bot, send me a message with either !insult, @insult or @insults for an insult! [https://github.com/AlexCatch/RobinCatFactsBot]");}, 200000);

    function onMessage(evt)
    {
        //recieved a message
        var jsonObject = JSON.parse(evt.data);
        if (jsonObject.type == "chat") {
            var regex = /^(!|@)in\s?(bot|sult?)/i;
            //recieved message is a chat message, compare contents
            if (regex.test(jsonObject.payload.body.toLowerCase())) {
                console.log("make said request");
                //message was direcxted to us
                makeRequest(function(fact) {
                    console.log(fact);
                    sendMessage(fact);
                });
            }
        }
    }
    
    function sendMessage(message) {
        $("#robinSendMessage > input[type='text']").val("");
        $("#robinSendMessage > input[type='text']").val(message);
        $("#robinSendMessage > input[type='submit']").click();
    }
    function makeRequest(callback) {
        GM_xmlhttpRequest ({
            method: 'GET',
            url:"http://quandyfactory.com/insult/json",
            onload: function (responseDetails) {
                // DO ALL RESPONSE PROCESSING HERE..
                var jsonResponse = JSON.parse(responseDetails.response);
                var fact = jsonResponse.insult;
              
                if (false) {
                    console.log("fact is too long");
                    makeRequest(function(fact){
                        sendMessage(fact);
                    });
                }else {
                    callback(fact + "...");
                }
            }
        });
    }
})();