Advertisement
Nik333

Fetch entire Discord conversation to json

Jan 9th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js');
  2. const client = new Discord.Client();
  3. const hideriToken = "TOKEN";
  4. const hideriId = "BOT USER ID"; // unused
  5. const fetchLimit = 50;
  6. const cdTime = 3000;
  7.  
  8. var allTexts = [];
  9. var bckFile = 0;
  10.  
  11. const fs = require('fs');
  12.  
  13. /**
  14.  * @returns Promise<string> contents
  15.  */
  16. function fileWrite(path, content, options) {
  17.     return new Promise((resolve, reject) => {
  18.         var callback = (err, data) => {
  19.             if (err) reject(err);
  20.             else resolve(data);
  21.         };
  22.         fs.writeFile(path, content, options ? options : callback, options ? callback : null);
  23.     });
  24. }
  25.  
  26. function sleep(time) {
  27.     return new Promise(resolve => setTimeout(resolve, time));
  28. }
  29.  
  30. client.on('ready', () => {
  31.     console.log(`Logged in as ${client.user.tag}!`);
  32.     let alexaChan = client.channels.get("ID"); // id of DM channel to back up
  33.  
  34.     async function processMessages(msgs) {
  35.         let last = undefined;
  36.         for (let entry of msgs.entries()) {
  37.             let msg = entry[1];
  38.             let id = entry[0];
  39.             last = id;
  40.             let msgObj = {
  41.                 id: id,
  42.                 content: msg.content,
  43.                 created: msg.createdAt,
  44.                 author: msg.author.tag
  45.             };
  46.             if (msg.attachments.size > 0) {
  47.                 let attachs = [];
  48.                 for (let attachEntry of msg.attachments.entries()) {
  49.                     let attach = attachEntry[1];
  50.                     attachs.push(attach.url);
  51.                 }
  52.                 msgObj.attachments = attachs;
  53.             }
  54.             allTexts.push(msgObj);
  55.         }
  56.         return last;
  57.     }
  58.  
  59.     async function fetchNext(before) {
  60.         let options = { limit: fetchLimit };
  61.         if (before) {
  62.             options.before = before;
  63.         }
  64.         alexaChan.fetchMessages(options).then(async (msgs) => {
  65.             newBefore = await processMessages(msgs);
  66.             console.log('targetFolder/backup' + bckFile + ".json");
  67.             await fileWrite('targetFolder/backup' + bckFile + ".json", JSON.stringify(allTexts), 'utf8');
  68.             bckFile++;
  69.             if (msgs.size >= fetchLimit) {
  70.                 await sleep(cdTime);
  71.                 fetchNext(newBefore);
  72.             }
  73.         });
  74.     }
  75.  
  76.     fetchNext();
  77. });
  78.  
  79. client.login(hideriToken);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement