Advertisement
dragonfree97

Untitled

Apr 2nd, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs"); // Filesystem access
  2. const https = require("https");
  3. var Utils = require('../modules/utils');
  4.  
  5. exports.main = function(message, mirrorWebhook) {
  6.     try {
  7.         var hook = mirrorWebhook;
  8.         var username = message.author.username;
  9.         var sanitisedUsername = username.replace(/(?:'| )/g,"_");
  10.         var avatarURL = message.author.avatarURL;
  11.         var content = message.content;
  12.         var avatars = [];
  13.         var filelocation = 'C:/Users/Robyn/Desktop/Projects/MlgMumbleBot/resources/avatars/'+sanitisedUsername+'.jpg';
  14.         var attachlocation = 'attachment://'+sanitisedUsername+'.jpg';
  15.                
  16.         // handle default avatars
  17.         if (avatarURL == "https://static.wikia.nocookie.net/messaging/scale-to-width-down/150") {
  18.             fs.copyFileSync('./resources/avatars/_default.jpg', './resources/avatars/'+sanitisedUsername+'.jpg');
  19.             avatars.push(sanitisedUsername);
  20.             console.log('Created default avatar for user '+username);
  21.         }
  22.        
  23.         var download = function(url, dest, cb) {
  24.             var file = fs.createWriteStream(dest);
  25.             var request = https.get(url, function(response) {
  26.                 response.pipe(file);
  27.                     file.on('finish', function() {
  28.                         file.close(cb); // close() is async, call cb after close completes.
  29.                     });
  30.                     }).on('error', function(err) { // Handle errors
  31.                 fs.unlink(dest); // Delete the file async. (But we don't check the result)
  32.                 if (cb) cb(err.message);
  33.             });
  34.         };
  35.        
  36.         fs.readdirSync("./resources/avatars/").forEach(file => {
  37.             avatars.push(file.substring(0,file.lastIndexOf(".")));
  38.         });
  39.        
  40.         var sendWebhook = function(webhook, content, username, avatarUrl) {
  41.             webhook.edit({ name: username, avatar: avatarUrl })
  42.                 .then(() => {
  43.                     webhook.send(content, { username: username })
  44.                     .then(message => console.log(`Sent message: ${message.content}`))
  45.                     .catch((error) => {
  46.                         console.log(error);
  47.                     });
  48.                 })
  49.                 .catch((error) => {
  50.                     console.log(error);
  51.                 });
  52.         }
  53.        
  54.         if (avatars.indexOf(sanitisedUsername) != -1) {
  55.             var lastModified = fs.statSync(filelocation).mtime;
  56.             var now = new Date().getTime();
  57.             if (now - lastModified > 3 * 60 * 60 * 1000 && avatarURL != "https://static.wikia.nocookie.net/messaging/scale-to-width-down/150") {
  58.                 download(avatarURL, filelocation, function() {
  59.                     console.log("Downloaded new avatar for "+username);
  60.                     sendWebhook(hook, content, username, filelocation);
  61.                 });
  62.             } else {
  63.                 sendWebhook(hook, content, username, filelocation);
  64.             }
  65.         } else {
  66.             download(avatarURL, './resources/avatars/'+username+'.jpg', function() {
  67.                 console.log("Downloaded new avatar for "+username);
  68.                 sendWebhook(hook, content, username, filelocation);
  69.             });
  70.         }
  71.     } catch (error) {
  72.         console.log(error);
  73.         return false;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement