Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Finds channels to check if channel should be monitored
  2. savedChannels.find({channelID: msg.channel.id.toString()})
  3.    .then(foundChannels => {
  4.       if (foundChannels.length === 0) return;
  5.       deleteMessage(msg, foundChannels[0].msgAmnt);
  6.    })
  7.    .catch(err => {
  8.       let title = "Failed to read the monitored channel collection";
  9.       errEmbed.embed(bot, title, "", 'dbEr', err);
  10.    });
  11.  
  12. function deleteMessage(message, msgDelAmount) {
  13.    //Finds all the messages by the user in a the channel so they can be deleted
  14.    messageCollection.find({userID: message.author.id.toString(), msgChannelID: message.channel.id})
  15.       .then(dbFetchedMsgs => {
  16.          //Fetches the discord message object for all the users messages
  17.          let discordMessage = dbFetchedMsgs
  18.             //Makes sure bot doesn't try to delete already deleted messages
  19.             .filter(d => d.messageDeleted === false)
  20.             //Finds the message ID of all the un-deleted messages
  21.             .map(m => m.messageID)
  22.             //Fetches the message object for every message ID
  23.             .map(m => bot.channels.get(msg.channel.id.toString()).fetchMessage(m));
  24.  
  25.          Promise.all(discordMessage)
  26.             .then(fetchedMsgObject => {
  27.                //Removes the last message from the array of message objects so it isn't deleted
  28.                let msgObjsToDel = fetchedMsgObject.slice(0, fetchedMsgObject.length - msgDelAmount);
  29.                msgObjsToDel.forEach(msgToDelete => {
  30.                   msgToDelete.delete()
  31.                      .then(() => {
  32.                         let msgCodeBlock = `\`\`\`${msgToDelete.content}\`\`\``;
  33.                         let title = "Deleted message";
  34.                         let body = `Deleted the following message by <@${msgToDelete.author.id}> as they posted more messages than are allowed in <#${msgToDelete.channel.id}>:\n${msgCodeBlock}`;
  35.                         logEmbed.embed(bot, title, body, 'dcOp');
  36.                      })
  37.                      .catch((err) => {
  38.                         msg.channel.send("I don't have permission to delete messages.");
  39.  
  40.                         let title = "Failed to delete message";
  41.                         let body = `Failed to delete the following message by <@${msgToDelete.author.id}>:\n\`\`\`${msgToDelete.content}\`\`\``;
  42.                         errEmbed.embed(bot, title, body, 'dcEr', err);
  43.                      });
  44.                });
  45.             })
  46.             .catch(err => {
  47.                if (err) {
  48.                   let title = "Failed to fetch discord message";
  49.                   let body = `Failed to fetch a discord message based on its database entry`;
  50.                   errEmbed.embed(bot, title, body, 'dcEr', err);
  51.                }
  52.             });
  53.       })
  54.       .catch(err => {
  55.          let title = "Failed to read the stored messages collection";
  56.          errEmbed.embed(bot, title, "", 'dbEr', err);
  57.       })
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement