Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. client.on("ready", () => {
  2.     console.log("ready")
  3.   console.log(date().format('YY MMM DD'))
  4.   if (!fs.existsSync("logs")){
  5.     fs.mkdirSync("logs");
  6. }
  7.  });
  8. //_____________________________________________________
  9. client.on("message",async message =>{
  10.  
  11.   //check so the channel is a text channel
  12.   if(message.channel.type == "text"){
  13.  
  14.   //check if the message has an attachment
  15.    if(message.attachments.size != 0){
  16.     let Url = message.attachments.first().url
  17.    
  18.     //creates the direcotries as necesserly for storeing the attachments
  19.     var dir = config.folder + "/" + message.guild.name + "/" + message.channel.name + "/"
  20.     mkdirp.sync(dir, function (err) {
  21.     if (err) console.error(err)
  22. });  
  23.    
  24.     //makes the filename a timestamp while preserveing the extention.
  25.     var extention = message.attachments.first().filename.split(".")
  26.     var NewFilename = date().format("LTS") + "." + extention[1]
  27.    
  28.  
  29.     //creates the downlaod function then calls it and pipe the attachment into designated folder/direcotry
  30.     var download = function(uri, filename, callback){
  31.     request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);}
  32.     download(Url, dir + NewFilename, function(err){
  33.       if (err)
  34.       throw err
  35.       })
  36.   }
  37.   //creates the datastring in .CSV format then passes it into the log function
  38.   logData = date().format("h a") + ";" + message.guild.name + ";" + message.channel.name + ";" + message.author.username + ";" + message.content + "\r\n"
  39.     log(logData,message.guild.name)
  40.  
  41.     }
  42. });
  43.  
  44. //function to write the datastring into .CSV file
  45. function log(data,guild){
  46. if (!fs.existsSync("logs/" + date().format('YYYY MMM DD'))){
  47.     fs.mkdirSync("logs/" + date().format('YYYY MMM DD'));
  48. }
  49.  
  50. fs.appendFile("logs/" + date().format('YYYY MMM DD') + "/"+ guild + ".csv", data, function (err) {
  51.   if (err) {
  52.     console.log(err)
  53.   } else {
  54.     // done
  55.   }
  56. })
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement