gravvy

Untitled

Jun 6th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. module.exports = {
  3.     blankFunction: () => {
  4.         //
  5.     },
  6.  
  7.     generateCode: (numbers, fs) => {
  8.         //Create code directory if it does not exist
  9.         if(!fs.existsSync("./code/"))makeFolder("./code/", fs);
  10.  
  11.         var rand = Math.random(9);
  12.         var code = "";
  13.  
  14.         do{
  15.             for (var i = 0; i < numbers; i++)
  16.             {
  17.                 var rand = Math.floor(Math.random() * 10);
  18.                 code += rand;
  19.             }
  20.  
  21.             var res = code.split('').sort(function(){return 0.5-Math.random()}).join('');
  22.         }while(fs.existsSync(`./code/${res}.json`) === true);
  23.  
  24.         return res;
  25.     },
  26.  
  27.     verifyChannel: (args, message, fs, fsextra, jsonfile, client) => {
  28.         var sender = message.member.user;//Get the sender name of the command
  29.  
  30.         if(args === null || args === "" || !args)
  31.         {
  32.             message.channel.send(`${sender}, To verify your channel please use ``/verify VerificationCode`` `);
  33.             return message.delete();
  34.         }
  35.  
  36.         var file = `./code/${args[0]}.json`;//Define the channel file name
  37.  
  38.         if(fs.existsSync(file) === true)
  39.         {
  40.             var contents = JSON.stringify(jsonfile.readFileSync(file));
  41.             var contents = JSON.parse(contents);
  42.             var code = args[0];//Get the channel name from the command
  43.             var codeVerified = null;
  44.             var type = contents.type;
  45.             var chn = contents.channel;
  46.  
  47.             if(code === contents.code)
  48.             {
  49.                 //Delete the code file for the channel
  50.                 fs.unlink(file, (err) => {
  51.                     if(err)return console.log(err);
  52.                 });
  53.  
  54.                 codeVerified = true;
  55.             }
  56.             else
  57.             {
  58.                 codeVerified = false;
  59.                 return message.channel.send(`${sender}, the code you entered is incorrect. Please check the code and try again.`);
  60.             }
  61.         }
  62.         else
  63.         {
  64.             return message.channel.send(`${sender}, I'm sorry but I can't seem to find that verification code.`);
  65.         }
  66.  
  67.         if(codeVerified === true)
  68.         {
  69.             var file = `./channels/${chn}.json`;//Define the channel file name
  70.  
  71.             if(type === "join")
  72.             {
  73.                 //Check to see if the channel file exists
  74.                 if(fs.existsSync(file) === false)
  75.                 {
  76.                     //If the channel file does not exist, create it and have the bot join the channel
  77.                     fsextra.ensureFile(file).then(() => {
  78.                         //Join the twitch channel
  79.                         client.join(`#${chn}`).then((data) => {
  80.                             //Tell the user the bot has joined the twitch channel
  81.                             return message.channel.send(`${sender}, RPBot9000 is joining channel **${chn}**! Please give the bot a few minutes to join this channel. Please /mod RPBot9000 to ensure the bot does not get banned.`);
  82.                         }).catch((err) => {return console.log(`JOIN Error: ${err} | ${chn}`);});
  83.                     }).catch(err => {
  84.                         //Log error in console
  85.                         return console.log(`ensureFile: ${err}`);
  86.                     });
  87.                 }
  88.                 else
  89.                 {
  90.                     //If the channel file does exist, tell the user it exists
  91.                     return message.channel.send(`${sender}, RPBot9000 is already in channel **${chn}**! Please /mod RPBot9000 to ensure the bot does not get banned.`);
  92.                 }
  93.             }
  94.  
  95.             if(type === "leave")
  96.             {
  97.                 //Check to see if the channel file exists
  98.                 if(fs.existsSync(file) === true)
  99.                 {
  100.                     //If the channel file does not exist, create it and have the bot join the channel
  101.                     fsextra.remove(file).then(() => {
  102.                         //Join the twitch channel
  103.                         client.part(`#${chn}`).then((data) => {return message.channel.send(`${sender}, RPBot9000 has left channel **${chn}**!`);}).catch((err) => {return console.log(`LEAVE Error: ${err} | ${chn}`);});
  104.                     })
  105.                     .catch(err => {
  106.                         //Log error in console
  107.                         return console.log(`ensureFile: ${err}`);
  108.                     });
  109.                 }
  110.                 else
  111.                 {
  112.                     //If the channel file does exist, tell the user it exists
  113.                     return message.channel.send(`${sender}, RPBot9000 is not in channel **${chn}**!`);
  114.                 }
  115.             }
  116.         }
  117.     },
  118.  
  119.     joinChannel: (args, message, fs, client, jsonfile, code) => {
  120.         if(args === null || args === "" || !args)
  121.         {
  122.             message.channel.send("To have the bot join your channel please use ``/join ChannelName``");
  123.             return message.delete();
  124.         }
  125.  
  126.         var sender = message.member.user;//Get the sender name of the command
  127.         var senderID = message.member.id;//Get the discord user id
  128.         var senderTag = message.member.user.tag;//Get the discord user id
  129.         var chn = args[0];//Get the channel name from the command
  130.         var code = code;//Generate a random 6 digit code
  131.         var file = `./code/${code}.json`;//Define the code file name for the channel
  132.         var obj = { code: `${code}`, channel: `${chn}`, discordID: `${senderID}`, discordName: `${senderTag}`, type: `join` };//Define what will be put into the verification code file
  133.         var canContinue = null;
  134.  
  135.         if(fs.existsSync(file) === false)
  136.         {
  137.             //Write verification code and channel name to file
  138.             jsonfile.writeFile(file, obj, {flag: "a"}, function (err) {
  139.                 if(err)return console.error(err);
  140.             });
  141.  
  142.             //Set canContinue to true after data has been written to file
  143.             canContinue = true;
  144.         }
  145.         else
  146.         {
  147.             canContinue = false;
  148.             return message.channel.send(`I'm sorry ${sender}, you have already requested a code. Please check your whispers on Twitch to retrieve the code.`);
  149.        }
  150.  
  151.        if(canContinue)
  152.        {
  153.            client.whisper(`${chn}`, `Your verification code is: ${code}`).then((data) => {
  154.                /*data returns [username, message]*/
  155.                return message.channel.send(`${sender}, A whisper has been sent to **${data[0]}** on Twitch, please use /verify with the code sent to you.`);
  156.            }).catch((err) => {
  157.                return console.log(`WHISPER Error: ${err} | ${chn}`);
  158.            });
  159.        }
  160.    },
  161.  
  162.    leaveChannel: (args, message, fs, client, jsonfile, code) => {
  163.        if(args === null || args === "" || !args)
  164.        {
  165.            message.channel.send("To have the bot leave your channel, please use ``/leave ChannelName``");
  166.            return message.delete();
  167.        }
  168.  
  169.        var sender = message.member.user;//Get the sender name of the command
  170.        var senderID = message.member.id;//Get the discord user id
  171.        var senderTag = message.member.user.tag;//Get the discord user id
  172.        var chn = args[0];//Get the channel name from the command
  173.        var code = code;//Generate a random 6 digit code
  174.        var file = `./code/${code}.json`;//Define the code file name for the channel
  175.        var obj = { code: `${code}`, channel: `${chn}`, user: `${senderID}`, discordName: `${senderTag}`, type: `leave` };//Define what will be put into the verification code file
  176.        var canContinue = null;
  177.  
  178.        if(fs.existsSync(file) === false)
  179.        {
  180.            //Write verification code and channel name to file
  181.            jsonfile.writeFile(file, obj, {flag: "a"}, function (err) {
  182.                if(err)return console.error(err);
  183.            });
  184.  
  185.            //Set canContinue to true after data has been written to file
  186.            canContinue = true;
  187.        }
  188.        else
  189.        {
  190.            canContinue = false;
  191.            return message.channel.send(`I'm sorry ${sender}, you have already requested a code. Please check your whispers on Twitch to retrieve the code.`);
  192.         }
  193.  
  194.         if(canContinue)
  195.         {
  196.             client.whisper(`${chn}`, `Your verification code is: ${code}`).then((data) => {
  197.                 /*data returns [username, message]*/
  198.                 return message.channel.send(`${sender}, A whisper has been sent to **${data[0]}** on Twitch, please use /verify with the code sent to you.`);
  199.             }).catch((err) => {
  200.                 return console.log(`WHISPER Error: ${err} | ${chn}`);
  201.             });
  202.         }
  203.     },
  204.  
  205.     banCount: (args, message, fs) => {
  206.         if(args === null || args === "" || !args)
  207.         {
  208.             message.channel.send("Please include the Twitch channel name. ``/bans ChannelName``");
  209.             return message.delete();
  210.         }
  211.  
  212.         var sender = message.member.user;//Get the sender name of the command
  213.         var canContinue = null;
  214.         var chn = args[0];
  215.         var file = `./channels/${chn}.json`;//Define the channel file name
  216.  
  217.         //Make sure channel file exist
  218.         if(fs.existsSync(file) === true)
  219.         {
  220.             canContinue = true;
  221.         }
  222.         else//If channel file does not exist
  223.         {
  224.             canContinue = false;
  225.             return message.channel.send(`I'm sorry ${sender}, it appears I don't monitor that channel.`);
  226.         }
  227.  
  228.         if(canContinue)
  229.         {
  230.             //Count number of lines in channel file
  231.             fs.readFile(file, 'utf8', (err, data) => {
  232.                 var banCount = data.split('\n').length;
  233.                 //Display ban count in chat
  234.                 return message.channel.send(`A total of **${banCount}** bans have been recorded in **${chn}**.`);
  235.             });
  236.         }
  237.     },
  238.  
  239.     checkBanned: (fs, path, message, args) => {
  240.         if(args === null || args === "" || !args)
  241.         {
  242.             message.channel.send("Please use ``/banned TwitchName``");
  243.             return message.delete();
  244.         }
  245.  
  246.         var userName = args[0];
  247.         var bannedChannels = new Array();
  248.  
  249.         function searchFilesInDirectory(dir, filter, ext) {
  250.             if (!fs.existsSync(dir)) {
  251.                 console.log(`Specified directory: ${dir} does not exist`);
  252.                 return;
  253.             }
  254.        
  255.             const files = getFilesInDirectory(dir, ext);
  256.        
  257.             files.forEach(file => {
  258.                 const fileContent = fs.readFileSync(file);
  259.        
  260.                 // We want full words, so we use full word boundary in regex.
  261.                 const regex = new RegExp('\\b' + filter + '\\b');
  262.                 if (regex.test(fileContent)) {
  263.                     file = file.slice(9, -5);
  264.                     bannedChannels.push(file);
  265.                 }
  266.             });
  267.         }
  268.  
  269.         function getFilesInDirectory(dir, ext) {
  270.             if (!fs.existsSync(dir)) {
  271.                 console.log(`Specified directory: ${dir} does not exist`);
  272.                 return;
  273.             }
  274.        
  275.             let files = [];
  276.             fs.readdirSync(dir).forEach(file => {
  277.                 const filePath = path.join(dir, file);
  278.                 const stat = fs.lstatSync(filePath);
  279.        
  280.                 // If we hit a directory, apply our function to that dir. If we hit a file, add it to the array of files.
  281.                 if (stat.isDirectory()) {
  282.                     const nestedFiles = getFilesInDirectory(filePath, ext);
  283.                     files = files.concat(nestedFiles);
  284.                 } else {
  285.                     if (path.extname(file) === ext) {
  286.                         files.push(filePath);
  287.                     }
  288.                 }
  289.             });
  290.        
  291.             return files;
  292.         }
  293.  
  294.         searchFilesInDirectory("./channels/", userName, ".json");
  295.         var channelNames = "";
  296.  
  297.         for(var n = 0; n < bannedChannels.length; n++)
  298.         {
  299.             //
  300.             channelNames += `**${bannedChannels[n]}**\n`;
  301.         }
  302.  
  303.         if(channelNames.length > 0)
  304.         {
  305.             return message.channel.send(`*__${userName}__* is currently banned in the following channels:\n${channelNames}`);
  306.         }
  307.         else
  308.         {
  309.             return message.channel.send(`*__${userName}__* is currently not banned in any channel.`);
  310.         }
  311.     },
  312.  
  313.     getUserMessages: (args, fs, message, sender) => {
  314.         //
  315.         if(!args || args === "" || args === undefined || args === null)return message.channel.send(`${sender}, Please include a Twitch username. Example: /history therealgravvy`);
  316.         var file = `./users/${args[0]}.txt`;
  317.         readLastLines = require('read-last-lines');
  318.  
  319.         if(fs.existsSync(file) === true)
  320.         {
  321.             //Get the last 25 lines from the user file
  322.             readLastLines.read(file, 25).then((lines) =>{
  323.                 return message.channel.send(`Last 25 messages from __${args[0]}__` + "```" + lines + "```");
  324.             }).catch((err) => {
  325.                 console.log(err);
  326.             });
  327.         }
  328.         else
  329.         {
  330.             return message.channel.send(`${sender}, Sorry that user does not exist in our database yet.`);
  331.         }
  332.     },
  333.  
  334.     removeFolder: (dir, fs, path) => {
  335.         if (!fs.existsSync(dir)) return;
  336.  
  337.         fs.readdirSync(dir).forEach((file) => {
  338.             const curPath = dir + path.sep + file;
  339.             if (fs.lstatSync(curPath).isDirectory())
  340.             removeFolder(curPath);
  341.             else
  342.             fs.unlinkSync(curPath);
  343.         });
  344.  
  345.         fs.rmdirSync(dir);
  346.         return true;
  347.     },
  348.  
  349.     makeFolder: (dir, fs) => {
  350.         if(fs.existsSync(dir)){
  351.             return true;
  352.         }else{
  353.             if(fs.mkdirSync(dir))
  354.             {
  355.                 return true;
  356.             }else{
  357.                 return false;
  358.             }
  359.         }
  360.     },
  361.  
  362.     banUser: (args, message, fs, jsonfile, sender) => {
  363.         return;
  364.         if(args === null || args === "" || !args)
  365.         {
  366.             message.channel.send("Please include the Twitch channel, User name and Reason. ``/ban ChannelName Username Reason``");
  367.             return message.delete();
  368.         }
  369.  
  370.         var channel = args[0];
  371.         var user = args[1];
  372.         var reason = args.slice(2).join(" ");
  373.         var d = new Date();//Create a new date from the current time and date
  374.         var day = d.getDate();//Get current day
  375.         var month = d.getMonth()+1;//Get current month
  376.         var year = d.getFullYear();//Get current year
  377.         var currDate = `${month}-${day}-${year}`;//Combine current day/month/year
  378.         var file = `./channels/${channel}.json`;
  379.  
  380.         //Check if channel file exists
  381.         if(fs.existsSync(file) === true)
  382.         {
  383.             //Get the contents of the channel file
  384.             console.log("content");
  385.             var content = JSON.stringify(jsonfile.readFileSync(file));
  386.             //var content = JSON.parse(JSON.stringify(jsonfile.readFileSync(file)));
  387.  
  388.             //Search through channel file and find the user
  389.             for(var i = 0; i < content.length; i++)
  390.             {
  391.                 //Check if the user exists in the channel file
  392.                 if(content[i].name === user)
  393.                 {
  394.                     //Define what to be written to the channel file
  395.                     var obj = { name: `${user}`, reason: `${reason}`, date: `${currDate}` };
  396.                     console.log("object1");
  397.  
  398.                     //Update the ban reason for the user banned
  399.                     jsonfile.writeFile(file, obj, {flag: "a"}, function (err) {
  400.                         if(err)return console.error(err);
  401.                     });
  402.  
  403.                     return message.channel.send(`${sender}, The user __${user}__ is already logged as banned in the channel __${channel}__. The ban reason has been updated.`);
  404.                 }
  405.                 else
  406.                 {
  407.                     //Define what to be written to the channel file
  408.                     var obj = { name: `${user}`, reason: `${reason}`, date: `${currDate}` };
  409.                     console.log("object2");
  410.  
  411.                     //Write to the channel file
  412.                     jsonfile.writeFile(file, obj, {flag: "a"}, function (err) {
  413.                         if(err)return console.error(err);
  414.                     });
  415.                    
  416.                     return message.channel.send(`${sender}, The user __${user}__ has been added to the log in channel __${channel}__ for the reason __${reason}__.`);
  417.                 }
  418.             }
  419.         }
  420.         else
  421.         {
  422.             return message.channel.send(`${sender}, Sorry but we don't log that channel.`);
  423.        }
  424.    }
  425. }
Add Comment
Please, Sign In to add comment