Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var BotName = 'RSIBot', // name of bot needs to match in both discord and xmpp for simplicity
- //discord settings
- DiscordChannel = '75630661951557632', //channel to relay xmpp in discord example: 75630661951557632
- DiscordEmail = "testsquadron@gmail.com", //discord email for login example: mymail@email.com
- DiscordPassword = "420TESTSquardon", //password for discord
- //xmpp settings
- XmppJid = 'RSIBot@xmpp.robertsspaceindustries.com', //xmpp nickname + server example mynic@myxmppserver.com
- XmppPassword = 'RSIXM+iqpv4mytu8rymqcrljf27x2h4z', //xmpp password
- XmppRoom = 'test@channels.robertsspaceindustries.com'; //xmpp room for bot to relay
- var streamers = ['supremetokyo', 'fallaste', 'farasalt', 'wykstrom', 'captain_richard', 'montoyaaa','russianj_test','badnewsbaron','kiltedfrog','princessflaafy','euthanize91','hpbraincase','grakees','skyyhawkyt','biogenx2b','anarck0s','slowfusegaming','admiral_nolan','programmersam','wykstrom','shadowvinez'];
- //---------------------------------------------------------------------------------------------------------
- //---------------------------------------------------------------------------------------------------------
- //discord connection
- //---------------------------------------------------------------------------------------------------------
- /*Variable area*/
- var Discordbot = require('discord.io');
- var request = require('request');
- var bot = new Discordbot({
- email: DiscordEmail,
- password: DiscordPassword,
- autorun: true
- });
- /*Event area*/
- bot.on("err", function (error) {
- console.log(error)
- });
- bot.on("ready", function (rawEvent) {
- console.log("Connected!");
- console.log("Logged in as: ");
- console.log(bot.username);
- console.log(bot.id);
- console.log("----------");
- setInterval(function () {
- var current = new Date();
- current.setSeconds(current.getSeconds() - 100);
- for (var i = 0, len = streamers.length; i < len; i++) {
- request('https://api.twitch.tv/kraken/streams/' + streamers[i], function (error, response, body) {
- if (!error && response.statusCode == 200) {
- var info = JSON.parse(body);
- if (info.stream != null) {
- console.log(info.stream.channel.url);
- var streamdate = new Date(info.stream.created_at);
- console.log(streamdate);
- console.log(current);
- if (streamdate > current) {
- bot.sendMessage({
- to: DiscordChannel,
- message: info.stream.channel.url + " is online playing " + info.stream.game + " check it out! ",
- nonce: "80085" //Optional
- }, function (response) { //CB Optional
- console.log(response.id); //Message ID
- });
- }
- }
- }
- });
- }
- }, 30000);
- });
- bot.on("message", function (user, userID, channelID, message, rawEvent) {
- console.log(user + " - " + userID);
- console.log("in " + channelID);
- message = bot.fixMessage(message);
- console.log(message);
- console.log("----------");
- if ((user !== BotName) && (channelID.toString() === DiscordChannel) && (undefined != user)) {
- conn.send(new xmpp.Element('message', { to: room_jid, type: 'groupchat' }).
- c('body').t(user.match(/[ -~]+/g) + " : " + message.match(/[ -~]+/g))//regex to prevent unicode characters from breaking node-xmpp
- );
- }
- if (message === "!ping") {
- sendMessages(channelID, ["Pong"]); //Sending a message with our helper function
- } else if (message === "!beer") {
- sendMessages(channelID, ["Cheers! ðŸº"]); //Sending a message with our helper function
- } else if (message === "!spank") {
- sendMessages(channelID, ["*sobs I was only trying to make everything right again* 😥"]); //Sending a message with our helper function
- } else if (message === "!ban") {
- sendMessages(channelID, ["Stop, Hammer time! 🔨"]); //Sending a message with our helper function
- } else if (message === "!rigged") {
- sendMessages(channelID, ["No its not I swear!!"]); //Sending a message with our helper function
- } else if (message === "picture") {
- sendFiles(channelID, ["fillsquare.png"]); //Sending a file with our helper function
- }
- });
- bot.on("presence", function (user, userID, status, rawEvent) {
- /*console.log(user + " is now: " + status);*/
- });
- bot.on("debug", function (rawEvent) {
- /*console.log(rawEvent)*/ //Logs every event
- });
- bot.on("disconnected", function () {
- console.log("Bot disconnected");
- /*bot.connect()*/ //Auto reconnect
- });
- /*Function declaration area*/
- function sendMessages(ID, messageArr, interval) {
- var len = messageArr.length;
- var callback;
- var resArr = [];
- typeof (arguments[2]) === 'function' ? callback = arguments[2] : callback = arguments[3];
- if (typeof (interval) !== 'number') interval = 250;
- function _sendMessages() {
- setTimeout(function () {
- if (messageArr.length > 0) {
- bot.sendMessage({
- to: ID,
- message: messageArr[0]
- }, function (res) {
- resArr.push(res);
- });
- messageArr.splice(0, 1);
- _sendMessages();
- }
- }, interval);
- }
- _sendMessages();
- var checkInt = setInterval(function () {
- if (resArr.length === len) {
- if (typeof (callback) === 'function') {
- callback(resArr);
- }
- clearInterval(checkInt);
- }
- }, 0);
- }
- function sendFiles(channelID, fileArr, interval) {
- var len = fileArr.length;
- var callback;
- var resArr = [];
- typeof (arguments[2]) === 'function' ? callback = arguments[2] : callback = arguments[3];
- if (typeof (interval) !== 'number') interval = 500;
- function _sendFiles() {
- setTimeout(function () {
- if (fileArr.length > 0) {
- bot.uploadFile({
- channel: channelID,
- file: fileArr[0]
- }, function (res) {
- resArr.push(res);
- });
- fileArr.splice(0, 1);
- _sendFiles();
- }
- }, interval);
- }
- _sendFiles();
- var checkInt = setInterval(function () {
- if (resArr.length === len) {
- if (typeof (callback) === 'function') {
- callback(resArr);
- }
- clearInterval(checkInt);
- }
- }, 0);
- }
- //---------------------------------------------------------------------------------------------------------
- //xmpp connection
- //---------------------------------------------------------------------------------------------------------
- var xmpp = require('node-xmpp'),
- sys = require('sys'),
- jid = XmppJid,
- password = XmppPassword,
- room_jid = XmppRoom,
- room_nick = BotName,
- conn = new xmpp.Client({
- jid: jid,
- password: password
- });
- conn.on('online', function () {
- console.log('online');
- var elm2 = new xmpp.Element('presence', { from: jid, to: room_jid }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc' }).up();
- conn.send(new xmpp.Element('presence', { to: room_jid + '/' + room_nick }).
- c('x', { xmlns: 'http://jabber.org/protocol/muc' })
- );
- conn.connection.socket.setTimeout(600000)
- conn.connection.socket.setKeepAlive(true, 10000)
- /*conn.send(new xmpp.Element('message', { to: room_jid, type: 'groupchat' }).
- c('body').t('test')
- );*/
- });
- conn.on('stanza', function (stanza) {
- if (stanza.is('message') &&
- // Important: never reply to errors!
- (stanza.attrs.type !== 'error') &&
- (stanza.attrs.from.toString() !== BotName)
- ) {
- // Swap addresses...
- stanza.attrs.to = stanza.attrs.from
- delete stanza.attrs.from
- // and send back
- var ssender = stanza.attrs.to.split("/");
- console.log('Sending response: ' + stanza.root().toString() + '\r\n-------------------')
- if ((ssender[1] !== BotName) && (undefined != ssender[1]) && (undefined != stanza.children[0].children)){
- try {
- bot.sendMessage({
- to: DiscordChannel,
- message: "" + ssender['1'] + "" + " : " + stanza.children[0].children[0],
- nonce: "80085" //Optional
- }, function (response) { //CB Optional
- console.log(response.id); //Message ID
- });
- }
- catch(e) { console.log("impressive break of the server",e); }
- }
- }
- });
- conn.on('error', function (e) {
- sys.puts(e);
- });
- bot.on('disconnected', function() {
- bot.connect();
- });
- //---------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement