Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var irc = require("irc");
- var winston = require("winston");
- var fs = require("fs");
- var readline = require("readline");
- var request = require("request");
- var express = require("express");
- var bodyParser = require('body-parser');
- var app = express();
- app.use(bodyParser.json()); // support json encoded bodies
- app.use(bodyParser.urlencoded({
- extended: true
- }));
- var port = 8080;
- app.use(function(req, res, next) {
- res.header("Access-Control-Allow-Origin", "*");
- res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
- next();
- });
- var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
- var modsFile = JSON.parse(fs.readFileSync('mod.json', 'utf8'));
- var rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout
- });
- app.listen(port);
- console.log('Server started! At http://localhost:' + port);
- app.post('/', function(req, res) {
- reloadConfig();
- // res.status(04).send("Hi!");
- // console.log('test');
- var command = req.body.command;
- var channels = config.channels;
- res.send('');
- if (command == 'reload') {
- reloadConfig();
- } else if (command == 'add') {
- channels.push(req.body.add);
- fs.writeFile('config.json', JSON.stringify(config), function(err) {
- if (err) return console.log(err);
- });
- reloadConfig();
- } else if (command == 'remove') {
- channels.push(req.body.add);
- fs.writeFile('config.json', JSON.stringify(config), function(err) {
- if (err) return console.log(err);
- });
- reloadConfig();
- } else if (command == 'send') {
- bot.say('#vaxitylol', req.body.mess);
- }
- });
- var settings = {
- channels: config.channels,
- server: "irc.twitch.tv",
- username: config.bot.username,
- nick: config.bot.username,
- password: config.bot.password,
- sasl: true
- };
- var logger = new(winston.Logger)({
- transports: [
- new(winston.transports.Console)({
- colorize: true,
- level: 'debug'
- }),
- new(winston.transports.File)({
- level: 'info',
- timestamp: true,
- filename: 'twitchChat.log',
- json: false
- })
- ]
- });
- // Get the lib
- // respond with "hello world" when a GET request is made to the homepage
- function getUser(channel) {
- var channelSplit = channel.split('');
- if (channelSplit[0] == '#') {
- var channelAlt = '';
- for (var i = 1; i < channelSplit.length; i++) {
- channelAlt += channelSplit[i];
- }
- return channelAlt;
- } else {
- return channel;
- }
- }
- function getMods(chanel) {
- var channel = getUser(chanel);
- request('http://tmi.twitch.tv/group/user/' + channel + '/chatters', function(error, response, body) {
- if (!error && response.statusCode == 200) {
- if (!(modsFile[chanel])) {
- modsFile[chanel] = {
- "mods": []
- };
- }
- var moderatorsJSON = JSON.parse(body);
- var currentMods = moderatorsJSON.chatters.moderators;
- var moderatorsListFromFile = modsFile[chanel].mods;
- var moderatorsComplete = moderatorsListFromFile.concat(currentMods);
- console.log(moderatorsComplete);
- fs.writeFile('mod.json', JSON.stringify(modsFile), function(err) {
- if (err) return console.log(err);
- });
- }
- });
- }
- function checkMod(person, mods) {
- var modsList = mods.chatters.moderators;
- for (var i = 0; i < modsList.length; i++) {
- if (person == modsList[i]) {
- return true;
- }
- }
- return false;
- }
- // Create the bot name
- var bot = new irc.Client(settings.server, settings.nick, {
- channels: [settings.channels + " " + settings.password],
- debug: true,
- password: settings.password,
- username: settings.nick
- });
- // Listen for joins
- // bot.addListener("join", function(channel, who) {
- // // Welcome them in!
- // global.channel = channel;
- // 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.");
- // });
- function funfact(channel) {
- // console.log("fact");
- var trivia = ['math', 'trivia', 'date', 'year'];
- var rand = getRandomInt(0, trivia.length - 1);
- var numb = trivia[rand];
- request('http://numbersapi.com/random/' + numb, function(error, response, body) {
- if (!error && response.statusCode == 200) {
- bot.say(channel, body);
- }
- });
- }
- function getRandomInt(min, max) {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- function reloadConfig() {
- try {
- mods = JSON.parse(fs.readFileSync('mod.json', 'utf8'));
- config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
- } catch (err) {
- console.log(err);
- }
- settings = {
- channels: config.channels,
- server: "irc.twitch.tv",
- username: config.bot.username,
- nick: config.bot.username,
- password: config.bot.password,
- sasl: true
- };
- bot = new irc.Client(settings.server, settings.nick, {
- channels: [settings.channels + " " + settings.password],
- debug: true,
- password: settings.password,
- username: settings.nick
- });
- }
- // Listen for any message, say to him/her in the room
- bot.addListener("message", function(from, to, text) {
- getMods(to);
- var textSplit = text.split(' ');
- var command = textSplit[1];
- // console.log(config.xtreameprogram2["!test"]);
- var messToSet = '';
- for (var i = 2; i < textSplit.length; i++) {
- messToSet += " " + textSplit[i];
- }
- logger.info('[From: ' + from + ', Channel: ' + to + '] Message: ' + text);
- if (textSplit[0] == '!add') {
- // console.log(global.isMod);
- // if (global.isMod) {
- setTimeout(function() {
- if (!(config[to])) {
- config[to] = {
- "commands": {}
- };
- config[to].commands[textSplit[1]] = {
- response: messToSet,
- lvl: "user"
- };
- setTimeout(function() {
- return;
- }, 5000);
- }
- // console.log(Object.keys(config.xtreameprogram2));
- else {
- config[to].commands[textSplit[1]] = {
- response: messToSet,
- lvl: "user"
- };
- }
- bot.say(to, from + ": Command added");
- fs.writeFile('config.json', JSON.stringify(config), function(err) {
- if (err) return console.log(err);
- });
- setTimeout(function() {
- return;
- }, 5000);
- }, 500);
- // }
- } else if (textSplit[0] == '!del') {
- // if (global.isMod) {
- setTimeout(function() {
- if (!(config[to].commands[textSplit[1]])) {
- bot.say(to, from + ": Command does not exist");
- } else {
- delete config[to].commands[textSplit[1]];
- bot.say(to, from + ": Command deleted");
- }
- fs.writeFile('config.json', JSON.stringify(config), function(err) {
- if (err) return console.log(err);
- });
- setTimeout(function() {
- return;
- }, 5000);
- }, 500);
- // }
- } else if (text == '!fact') {
- setTimeout(function() {
- funfact(to);
- setTimeout(function() {
- return;
- }, 5000);
- }, 500);
- } else {
- if (config[to]) {
- setTimeout(function() {
- var commands = Object.keys(config[to].commands);
- for (var r = 0; r < commands.length; r++) {
- if (commands[r] == textSplit[0]) {
- if (config[to].commands[commands[r]].lvl == 'user') {
- bot.say(to, config[to].commands[commands[r]].response);
- break;
- } else if (config[to].commands[commands[r]].lvl == 'mod') {
- if (global.isMod === true) {
- bot.say(to, config[to].commands[commands[r]].response);
- break;
- }
- } else if (config[to].commands[commands[r]].lvl == 'owner') {
- if (to == from) {
- bot.say(to, config[to].commands[commands[r]].response);
- break;
- }
- }
- }
- }
- setTimeout(function() {
- return;
- }, 5000);
- }, 500);
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement