gravvy

Untitled

Jun 6th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. module.exports = {
  3.     onBan: (channel, username, reason, botconfig, bot, fs, client, jsonfile) => {
  4.         let chatBans = botconfig.chatBans;//Define the channel name to submit channel bans to
  5.         let logChannel = bot.channels.find(c => c.name === chatBans);//Get the channel from discord
  6.         var chn = channel.slice(1);//Remove the # from the twitch channel name
  7.         var usrnme = username;//Get the username of the user that was banned
  8.         var file = `./channels/${channel.slice(1)}.json`;//Define the channel file name to be created
  9.         var d = new Date();//Create a new date from the current time and date
  10.         var day = d.getDate();//Get current day
  11.         var month = d.getMonth()+1;//Get current month
  12.         var year = d.getFullYear();//Get current year
  13.         var dte = `${month}-${day}-${year}`;//Combine current day/month/year
  14.         var obj = { name: `${usrnme}`, reason: `${reason}`, date: `${dte}` };//Define what will be put into the channel file
  15.  
  16.         //If the bot is banned from a channel
  17.         if(usrnme === "rpbot9000" || usrnme === "RPBot9000")
  18.         {
  19.             //Delete that channels file
  20.             fs.unlink(file, (err) => {
  21.                 if(err)return console.log(err);
  22.             });
  23.  
  24.             //Manually leave channel to make sure the bot does not reconnect
  25.             return client.part(`#${chn}`).then((data) => {return console.log(`RPBot9000 was banned from channel ${chn}`)}).catch((err) => {return console.log(`LEAVE Error: ${err} | ${chn}`);});
  26.         }
  27.  
  28.         //Write banned user information to the channel file
  29.         jsonfile.writeFile(file, obj, {flag: "a"}, function (err) {
  30.             if(err)return console.error(err);
  31.         });
  32.        
  33.         //Log the banned user in discord
  34.         return logChannel.send("```diff"+`\n-Banned_User\n+[Channel:] ${chn}\n+[User:] ${usrnme}\n+[Date:] ${dte}`+"```");
  35.     },
  36.  
  37.     onTimeout: (channel, username, reason, duration, botconfig, bot, fs, client, jsonfile) => {
  38.         let chatBans = botconfig.chatBans;//Define the channel name to submit channel timeout to
  39.         let logChannel = bot.channels.find(c => c.name === chatBans);//Get the channel from discord
  40.         var chn = channel.slice(1);//Remove the # from the twitch channel name
  41.         var usrnme = username;//Get the username of the user that was timed out
  42.  
  43.         var file = `./channels/${channel.slice(1)}.json`;//Define the channel file name to be created
  44.         var d = new Date();//Create a new date from the current time and date
  45.         var day = d.getDate();//Get current day
  46.         var month = d.getMonth()+1;//Get current month
  47.         var year = d.getFullYear();//Get current year
  48.         var dte = `${month}-${day}-${year}`;//Combine current day/month/year
  49.         var obj = { name: `${usrnme}`, reason: `${reason}`, date: `${dte}` };//Define what will be put into the channel file
  50.  
  51.         //Write timed out user information to the channel file
  52.         jsonfile.writeFile(file, obj, {flag: "a"}, function (err) {
  53.             if(err)return console.error(err);
  54.         });
  55.        
  56.         //Log the timed out user in discord
  57.         return logChannel.send("```css"+`\n.TimedOut_User\n+[Channel:] ${chn}\n+[User:] ${usrnme}\n+[Duration:] ${duration}\n+[Date:] ${dte}`+"```");
  58.     },
  59.  
  60.     onWhisper: (from, message, self, client) => {
  61.         //Send a reply to the user that sent the whisper
  62.         return client.whisper(from, "Uh oh! I'm just a bot! If you are intrested in adding this bot to your twitch channel, please visit us on discord at https://discord.io/rpbot9000").then((data) => {
  63.             // data returns [username, message]
  64.         }).catch((err) => {
  65.             console.log(err);//Log all errors
  66.         });
  67.     },
  68.  
  69.     onDisconnect: (options) => {
  70.         for(var i = 0; i < options.channels.length; i++)
  71.         {
  72.             var chan = options.channels[i];
  73.             console.log(chan, "RPBot9000 disconnected.");//Log in the console that the bot has disconnected from a twitch channel
  74.         }
  75.     },
  76.  
  77.     onRoomstate: (channel, fsextra) => {
  78.         //Define the channel file
  79.         var file = `./channels/${channel.slice(1)}.json`;
  80.  
  81.         //Check to see if channels json file exists
  82.         fsextra.ensureFile(file).then(() => {
  83.             //Nothing needs to be done here
  84.         }).catch(err => {
  85.             return console.log(`ensureFile: ${err}`);
  86.         });
  87.     },
  88.  
  89.     onConnect: (options) => {
  90.         //Create counter for connected channels
  91.         var conChannels = 0;
  92.  
  93.         //Logs connection status to channel(s)
  94.         for(var i = 0; i < options.channels.length; i++)
  95.         {
  96.             conChannels += 1;//Increase the connected channels counter
  97.             /*var chan = options.channels[i];
  98.             console.log(chan, "is connected.");//Log connection status to the console*/
  99.         }
  100.  
  101.         console.log(`Connected to [${conChannels}] Twitch channels.`);//Log the amount of channels connected in console
  102.     },
  103.  
  104.     logMessages: (channel, userstate, message, fs, fsextra) => {
  105.         //Define the user file
  106.         var file = `./users/${userstate.username}.txt`;
  107.  
  108.         //Define the contents to be written to file
  109.         var contents = `${channel} | ${userstate.username} | ${message}`;
  110.  
  111.         //get the line count of the user file
  112.         function lineCount(fs, file){
  113.             //Count number of lines in channel file
  114.             fs.readFile(file, 'utf8', (err, data) => {
  115.                 var lineCount = data.split('\n').length;
  116.                 return lineCount;
  117.             });
  118.         }
  119.  
  120.         //Create the user file
  121.         function createFile(fsextra, fs, file, contents){
  122.             //If users file does not exist, create it
  123.             fsextra.ensureFile(file).then(() => {
  124.                 //Write chat data to user file after user file has been created
  125.                 return writeToFile(fs, file, contents);
  126.             }).catch(err => {
  127.                 return console.log(`ensureFile: ${err}`);
  128.             });
  129.         }
  130.  
  131.         //Write data to the user file
  132.         function writeToFile(fs, file, contents){
  133.             //Write chat data to user file
  134.             fs.writeFile(file, contents, (err) => {
  135.                 if(err)return console.log(err);
  136.             });
  137.         }
  138.  
  139.         //Write data to the user file
  140.         function appendToFile(fs, file, contents){
  141.             //Write chat data to user file
  142.             fs.appendFile(file, `\n${contents}`, (err) => {
  143.                 if(err)return console.log(err);
  144.             });
  145.         }
  146.  
  147.         //Check to see if users text file exists
  148.         if(fs.existsSync(file) === true)
  149.         {
  150.             //Get the line count in the user file
  151.             lineCount(fs, file);
  152.  
  153.             //Write to the user file
  154.             return appendToFile(fs, file, contents);
  155.         }
  156.         else
  157.         {
  158.             //Create the user file and write to it
  159.             createFile(fsextra, fs, file, contents);
  160.         }
  161.     }
  162. }
Add Comment
Please, Sign In to add comment