Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.27 KB | None | 0 0
  1. var BotName = 'RSIBot', // name of bot needs to match in both discord and xmpp for simplicity
  2. //discord settings
  3. DiscordChannel = '75630661951557632', //channel to relay xmpp in discord example: 75630661951557632
  4. DiscordEmail = "testsquadron@gmail.com", //discord email for login example: mymail@email.com
  5. DiscordPassword = "420TESTSquardon", //password for discord
  6. //xmpp settings
  7. XmppJid = 'RSIBot@xmpp.robertsspaceindustries.com', //xmpp nickname + server example mynic@myxmppserver.com
  8. XmppPassword = 'RSIXM+iqpv4mytu8rymqcrljf27x2h4z', //xmpp password
  9. XmppRoom = 'test@channels.robertsspaceindustries.com'; //xmpp room for bot to relay
  10.  
  11. 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'];
  12. //---------------------------------------------------------------------------------------------------------
  13. //---------------------------------------------------------------------------------------------------------
  14. //discord connection
  15. //---------------------------------------------------------------------------------------------------------
  16. /*Variable area*/
  17. var Discordbot = require('discord.io');
  18. var request = require('request');
  19. var bot = new Discordbot({
  20. email: DiscordEmail,
  21. password: DiscordPassword,
  22. autorun: true
  23. });
  24. /*Event area*/
  25. bot.on("err", function (error) {
  26. console.log(error)
  27. });
  28.  
  29. bot.on("ready", function (rawEvent) {
  30. console.log("Connected!");
  31. console.log("Logged in as: ");
  32. console.log(bot.username);
  33. console.log(bot.id);
  34. console.log("----------");
  35. setInterval(function () {
  36. var current = new Date();
  37. current.setSeconds(current.getSeconds() - 100);
  38.  
  39. for (var i = 0, len = streamers.length; i < len; i++) {
  40. request('https://api.twitch.tv/kraken/streams/' + streamers[i], function (error, response, body) {
  41. if (!error && response.statusCode == 200) {
  42. var info = JSON.parse(body);
  43. if (info.stream != null) {
  44. console.log(info.stream.channel.url);
  45. var streamdate = new Date(info.stream.created_at);
  46. console.log(streamdate);
  47. console.log(current);
  48. if (streamdate > current) {
  49. bot.sendMessage({
  50. to: DiscordChannel,
  51. message: info.stream.channel.url + " is online playing " + info.stream.game + " check it out! ",
  52. nonce: "80085" //Optional
  53. }, function (response) { //CB Optional
  54. console.log(response.id); //Message ID
  55. });
  56. }
  57. }
  58. }
  59. });
  60. }
  61. }, 30000);
  62. });
  63.  
  64.  
  65. bot.on("message", function (user, userID, channelID, message, rawEvent) {
  66. console.log(user + " - " + userID);
  67. console.log("in " + channelID);
  68. message = bot.fixMessage(message);
  69. console.log(message);
  70. console.log("----------");
  71. if ((user !== BotName) && (channelID.toString() === DiscordChannel) && (undefined != user)) {
  72. conn.send(new xmpp.Element('message', { to: room_jid, type: 'groupchat' }).
  73. c('body').t(user.match(/[ -~]+/g) + " : " + message.match(/[ -~]+/g))//regex to prevent unicode characters from breaking node-xmpp
  74. );
  75. }
  76. if (message === "!ping") {
  77. sendMessages(channelID, ["Pong"]); //Sending a message with our helper function
  78. } else if (message === "!beer") {
  79. sendMessages(channelID, ["Cheers! 🍺"]); //Sending a message with our helper function
  80. } else if (message === "!spank") {
  81. sendMessages(channelID, ["*sobs I was only trying to make everything right again* 😥"]); //Sending a message with our helper function
  82. } else if (message === "!ban") {
  83. sendMessages(channelID, ["Stop, Hammer time! 🔨"]); //Sending a message with our helper function
  84. } else if (message === "!rigged") {
  85. sendMessages(channelID, ["No its not I swear!!"]); //Sending a message with our helper function
  86. } else if (message === "picture") {
  87. sendFiles(channelID, ["fillsquare.png"]); //Sending a file with our helper function
  88. }
  89. });
  90.  
  91. bot.on("presence", function (user, userID, status, rawEvent) {
  92. /*console.log(user + " is now: " + status);*/
  93. });
  94.  
  95. bot.on("debug", function (rawEvent) {
  96. /*console.log(rawEvent)*/ //Logs every event
  97. });
  98.  
  99. bot.on("disconnected", function () {
  100. console.log("Bot disconnected");
  101. /*bot.connect()*/ //Auto reconnect
  102. });
  103.  
  104. /*Function declaration area*/
  105. function sendMessages(ID, messageArr, interval) {
  106. var len = messageArr.length;
  107. var callback;
  108. var resArr = [];
  109. typeof (arguments[2]) === 'function' ? callback = arguments[2] : callback = arguments[3];
  110. if (typeof (interval) !== 'number') interval = 250;
  111.  
  112. function _sendMessages() {
  113. setTimeout(function () {
  114. if (messageArr.length > 0) {
  115. bot.sendMessage({
  116. to: ID,
  117. message: messageArr[0]
  118. }, function (res) {
  119. resArr.push(res);
  120. });
  121. messageArr.splice(0, 1);
  122. _sendMessages();
  123. }
  124. }, interval);
  125. }
  126. _sendMessages();
  127.  
  128. var checkInt = setInterval(function () {
  129. if (resArr.length === len) {
  130. if (typeof (callback) === 'function') {
  131. callback(resArr);
  132. }
  133. clearInterval(checkInt);
  134. }
  135. }, 0);
  136. }
  137.  
  138. function sendFiles(channelID, fileArr, interval) {
  139. var len = fileArr.length;
  140. var callback;
  141. var resArr = [];
  142. typeof (arguments[2]) === 'function' ? callback = arguments[2] : callback = arguments[3];
  143. if (typeof (interval) !== 'number') interval = 500;
  144.  
  145. function _sendFiles() {
  146. setTimeout(function () {
  147. if (fileArr.length > 0) {
  148. bot.uploadFile({
  149. channel: channelID,
  150. file: fileArr[0]
  151. }, function (res) {
  152. resArr.push(res);
  153. });
  154. fileArr.splice(0, 1);
  155. _sendFiles();
  156. }
  157. }, interval);
  158. }
  159. _sendFiles();
  160.  
  161. var checkInt = setInterval(function () {
  162. if (resArr.length === len) {
  163. if (typeof (callback) === 'function') {
  164. callback(resArr);
  165. }
  166. clearInterval(checkInt);
  167. }
  168. }, 0);
  169. }
  170. //---------------------------------------------------------------------------------------------------------
  171. //xmpp connection
  172. //---------------------------------------------------------------------------------------------------------
  173. var xmpp = require('node-xmpp'),
  174. sys = require('sys'),
  175. jid = XmppJid,
  176. password = XmppPassword,
  177. room_jid = XmppRoom,
  178. room_nick = BotName,
  179. conn = new xmpp.Client({
  180. jid: jid,
  181. password: password
  182. });
  183.  
  184. conn.on('online', function () {
  185. console.log('online');
  186. var elm2 = new xmpp.Element('presence', { from: jid, to: room_jid }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc' }).up();
  187.  
  188. conn.send(new xmpp.Element('presence', { to: room_jid + '/' + room_nick }).
  189. c('x', { xmlns: 'http://jabber.org/protocol/muc' })
  190. );
  191. conn.connection.socket.setTimeout(600000)
  192. conn.connection.socket.setKeepAlive(true, 10000)
  193. /*conn.send(new xmpp.Element('message', { to: room_jid, type: 'groupchat' }).
  194. c('body').t('test')
  195. );*/
  196. });
  197.  
  198. conn.on('stanza', function (stanza) {
  199. if (stanza.is('message') &&
  200. // Important: never reply to errors!
  201. (stanza.attrs.type !== 'error') &&
  202. (stanza.attrs.from.toString() !== BotName)
  203. ) {
  204. // Swap addresses...
  205. stanza.attrs.to = stanza.attrs.from
  206.  
  207. delete stanza.attrs.from
  208. // and send back
  209. var ssender = stanza.attrs.to.split("/");
  210. console.log('Sending response: ' + stanza.root().toString() + '\r\n-------------------')
  211.  
  212. if ((ssender[1] !== BotName) && (undefined != ssender[1]) && (undefined != stanza.children[0].children)){
  213. try {
  214. bot.sendMessage({
  215. to: DiscordChannel,
  216. message: "" + ssender['1'] + "" + " : " + stanza.children[0].children[0],
  217. nonce: "80085" //Optional
  218. }, function (response) { //CB Optional
  219. console.log(response.id); //Message ID
  220. });
  221. }
  222. catch(e) { console.log("impressive break of the server",e); }
  223. }
  224. }
  225. });
  226.  
  227. conn.on('error', function (e) {
  228. sys.puts(e);
  229. });
  230.  
  231. bot.on('disconnected', function() {
  232. bot.connect();
  233. });
  234. //---------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement