Advertisement
Guest User

bot

a guest
Jan 22nd, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. var irc = require("irc");
  2. var winston = require("winston");
  3. var fs = require("fs");
  4. var readline = require("readline");
  5. var request = require("request");
  6. var express = require("express");
  7. var bodyParser = require('body-parser');
  8.  
  9. var app = express();
  10. app.use(bodyParser.json()); // support json encoded bodies
  11. app.use(bodyParser.urlencoded({
  12. extended: true
  13. }));
  14. var port = 8080;
  15.  
  16. app.use(function(req, res, next) {
  17. res.header("Access-Control-Allow-Origin", "*");
  18. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  19. next();
  20. });
  21.  
  22. var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
  23. var modsFile = JSON.parse(fs.readFileSync('mod.json', 'utf8'));
  24.  
  25. var rl = readline.createInterface({
  26. input: process.stdin,
  27. output: process.stdout
  28. });
  29.  
  30. app.listen(port);
  31. console.log('Server started! At http://localhost:' + port);
  32.  
  33. app.post('/', function(req, res) {
  34. reloadConfig();
  35. // res.status(04).send("Hi!");
  36. // console.log('test');
  37. var command = req.body.command;
  38. var channels = config.channels;
  39. res.send('');
  40.  
  41. if (command == 'reload') {
  42. reloadConfig();
  43. } else if (command == 'add') {
  44. channels.push(req.body.add);
  45. fs.writeFile('config.json', JSON.stringify(config), function(err) {
  46. if (err) return console.log(err);
  47. });
  48. reloadConfig();
  49. } else if (command == 'remove') {
  50.  
  51. channels.push(req.body.add);
  52. fs.writeFile('config.json', JSON.stringify(config), function(err) {
  53. if (err) return console.log(err);
  54. });
  55. reloadConfig();
  56. } else if (command == 'send') {
  57. bot.say('#vaxitylol', req.body.mess);
  58. }
  59.  
  60.  
  61. });
  62.  
  63.  
  64. var settings = {
  65. channels: config.channels,
  66. server: "irc.twitch.tv",
  67. username: config.bot.username,
  68. nick: config.bot.username,
  69. password: config.bot.password,
  70. sasl: true
  71. };
  72.  
  73. var logger = new(winston.Logger)({
  74. transports: [
  75. new(winston.transports.Console)({
  76. colorize: true,
  77. level: 'debug'
  78. }),
  79. new(winston.transports.File)({
  80. level: 'info',
  81. timestamp: true,
  82. filename: 'twitchChat.log',
  83. json: false
  84. })
  85. ]
  86. });
  87.  
  88.  
  89.  
  90.  
  91. // Get the lib
  92.  
  93. // respond with "hello world" when a GET request is made to the homepage
  94. function getUser(channel) {
  95. var channelSplit = channel.split('');
  96. if (channelSplit[0] == '#') {
  97. var channelAlt = '';
  98. for (var i = 1; i < channelSplit.length; i++) {
  99. channelAlt += channelSplit[i];
  100. }
  101. return channelAlt;
  102. } else {
  103. return channel;
  104. }
  105. }
  106.  
  107. function getMods(chanel) {
  108. var channel = getUser(chanel);
  109.  
  110.  
  111. request('http://tmi.twitch.tv/group/user/' + channel + '/chatters', function(error, response, body) {
  112. if (!error && response.statusCode == 200) {
  113. if (!(modsFile[chanel])) {
  114. modsFile[chanel] = {
  115. "mods": []
  116. };
  117. }
  118. var moderatorsJSON = JSON.parse(body);
  119. var currentMods = moderatorsJSON.chatters.moderators;
  120. var moderatorsListFromFile = modsFile[chanel].mods;
  121. var moderatorsComplete = moderatorsListFromFile.concat(currentMods);
  122. console.log(moderatorsComplete);
  123.  
  124.  
  125. fs.writeFile('mod.json', JSON.stringify(modsFile), function(err) {
  126. if (err) return console.log(err);
  127. });
  128.  
  129. }
  130. });
  131.  
  132.  
  133. }
  134.  
  135.  
  136. function checkMod(person, mods) {
  137. var modsList = mods.chatters.moderators;
  138. for (var i = 0; i < modsList.length; i++) {
  139. if (person == modsList[i]) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145.  
  146.  
  147. // Create the bot name
  148. var bot = new irc.Client(settings.server, settings.nick, {
  149. channels: [settings.channels + " " + settings.password],
  150. debug: true,
  151. password: settings.password,
  152. username: settings.nick
  153. });
  154.  
  155. // Listen for joins
  156. // bot.addListener("join", function(channel, who) {
  157. // // Welcome them in!
  158. // global.channel = channel;
  159. // bot.say(channel, "Hello! I am a ChatBot designed for Twitch. I have many functionalities and type !help to begin. However, I may not be able to do a couple things without being mod.");
  160. // });
  161.  
  162. function funfact(channel) {
  163. // console.log("fact");
  164. var trivia = ['math', 'trivia', 'date', 'year'];
  165. var rand = getRandomInt(0, trivia.length - 1);
  166. var numb = trivia[rand];
  167.  
  168. request('http://numbersapi.com/random/' + numb, function(error, response, body) {
  169. if (!error && response.statusCode == 200) {
  170. bot.say(channel, body);
  171. }
  172. });
  173. }
  174.  
  175. function getRandomInt(min, max) {
  176. return Math.floor(Math.random() * (max - min + 1)) + min;
  177. }
  178.  
  179. function reloadConfig() {
  180. try {
  181. mods = JSON.parse(fs.readFileSync('mod.json', 'utf8'));
  182. config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
  183. } catch (err) {
  184. console.log(err);
  185. }
  186. settings = {
  187. channels: config.channels,
  188. server: "irc.twitch.tv",
  189. username: config.bot.username,
  190. nick: config.bot.username,
  191. password: config.bot.password,
  192. sasl: true
  193. };
  194. bot = new irc.Client(settings.server, settings.nick, {
  195. channels: [settings.channels + " " + settings.password],
  196. debug: true,
  197. password: settings.password,
  198. username: settings.nick
  199. });
  200. }
  201.  
  202. // Listen for any message, say to him/her in the room
  203. bot.addListener("message", function(from, to, text) {
  204.  
  205. getMods(to);
  206.  
  207. var textSplit = text.split(' ');
  208. var command = textSplit[1];
  209. // console.log(config.xtreameprogram2["!test"]);
  210. var messToSet = '';
  211. for (var i = 2; i < textSplit.length; i++) {
  212. messToSet += " " + textSplit[i];
  213. }
  214.  
  215. logger.info('[From: ' + from + ', Channel: ' + to + '] Message: ' + text);
  216. if (textSplit[0] == '!add') {
  217. // console.log(global.isMod);
  218. // if (global.isMod) {
  219. setTimeout(function() {
  220. if (!(config[to])) {
  221. config[to] = {
  222. "commands": {}
  223. };
  224. config[to].commands[textSplit[1]] = {
  225. response: messToSet,
  226. lvl: "user"
  227. };
  228. setTimeout(function() {
  229. return;
  230. }, 5000);
  231. }
  232. // console.log(Object.keys(config.xtreameprogram2));
  233. else {
  234. config[to].commands[textSplit[1]] = {
  235. response: messToSet,
  236. lvl: "user"
  237. };
  238. }
  239. bot.say(to, from + ": Command added");
  240. fs.writeFile('config.json', JSON.stringify(config), function(err) {
  241. if (err) return console.log(err);
  242. });
  243. setTimeout(function() {
  244. return;
  245. }, 5000);
  246. }, 500);
  247.  
  248. // }
  249. } else if (textSplit[0] == '!del') {
  250. // if (global.isMod) {
  251. setTimeout(function() {
  252. if (!(config[to].commands[textSplit[1]])) {
  253. bot.say(to, from + ": Command does not exist");
  254. } else {
  255. delete config[to].commands[textSplit[1]];
  256. bot.say(to, from + ": Command deleted");
  257. }
  258.  
  259. fs.writeFile('config.json', JSON.stringify(config), function(err) {
  260. if (err) return console.log(err);
  261. });
  262. setTimeout(function() {
  263. return;
  264. }, 5000);
  265. }, 500);
  266. // }
  267. } else if (text == '!fact') {
  268. setTimeout(function() {
  269. funfact(to);
  270. setTimeout(function() {
  271. return;
  272. }, 5000);
  273. }, 500);
  274. } else {
  275. if (config[to]) {
  276. setTimeout(function() {
  277. var commands = Object.keys(config[to].commands);
  278. for (var r = 0; r < commands.length; r++) {
  279. if (commands[r] == textSplit[0]) {
  280. if (config[to].commands[commands[r]].lvl == 'user') {
  281. bot.say(to, config[to].commands[commands[r]].response);
  282. break;
  283. } else if (config[to].commands[commands[r]].lvl == 'mod') {
  284. if (global.isMod === true) {
  285. bot.say(to, config[to].commands[commands[r]].response);
  286. break;
  287. }
  288. } else if (config[to].commands[commands[r]].lvl == 'owner') {
  289. if (to == from) {
  290. bot.say(to, config[to].commands[commands[r]].response);
  291. break;
  292. }
  293. }
  294. }
  295. }
  296. setTimeout(function() {
  297. return;
  298. }, 5000);
  299. }, 500);
  300. }
  301. }
  302.  
  303. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement