Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Finds channels to check if channel should be monitored
- savedChannels.find({channelID: msg.channel.id.toString()})
- .then(foundChannels => {
- if (foundChannels.length === 0) return;
- deleteMessage(msg, foundChannels[0].msgAmnt);
- })
- .catch(err => {
- let title = "Failed to read the monitored channel collection";
- errEmbed.embed(bot, title, "", 'dbEr', err);
- });
- function deleteMessage(message, msgDelAmount) {
- //Finds all the messages by the user in a the channel so they can be deleted
- messageCollection.find({userID: message.author.id.toString(), msgChannelID: message.channel.id})
- .then(dbFetchedMsgs => {
- //Fetches the discord message object for all the users messages
- let discordMessage = dbFetchedMsgs
- //Makes sure bot doesn't try to delete already deleted messages
- .filter(d => d.messageDeleted === false)
- //Finds the message ID of all the un-deleted messages
- .map(m => m.messageID)
- //Fetches the message object for every message ID
- .map(m => bot.channels.get(msg.channel.id.toString()).fetchMessage(m));
- Promise.all(discordMessage)
- .then(fetchedMsgObject => {
- //Removes the last message from the array of message objects so it isn't deleted
- let msgObjsToDel = fetchedMsgObject.slice(0, fetchedMsgObject.length - msgDelAmount);
- msgObjsToDel.forEach(msgToDelete => {
- msgToDelete.delete()
- .then(() => {
- let msgCodeBlock = `\`\`\`${msgToDelete.content}\`\`\``;
- let title = "Deleted message";
- let body = `Deleted the following message by <@${msgToDelete.author.id}> as they posted more messages than are allowed in <#${msgToDelete.channel.id}>:\n${msgCodeBlock}`;
- logEmbed.embed(bot, title, body, 'dcOp');
- })
- .catch((err) => {
- msg.channel.send("I don't have permission to delete messages.");
- let title = "Failed to delete message";
- let body = `Failed to delete the following message by <@${msgToDelete.author.id}>:\n\`\`\`${msgToDelete.content}\`\`\``;
- errEmbed.embed(bot, title, body, 'dcEr', err);
- });
- });
- })
- .catch(err => {
- if (err) {
- let title = "Failed to fetch discord message";
- let body = `Failed to fetch a discord message based on its database entry`;
- errEmbed.embed(bot, title, body, 'dcEr', err);
- }
- });
- })
- .catch(err => {
- let title = "Failed to read the stored messages collection";
- errEmbed.embed(bot, title, "", 'dbEr', err);
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement