How to handle message deletions when the message in question is already gone?

Helo! I ran into an issue where a message was already deleted and i cant get the bot to not throw an error "Unknown Message".
const { PermissionsBitField } = require('discord.js');

const BridgedChannel = require('../../database/models/bridgedChannel');

const MessageLink = require('../../database/models/messageLink');

async function getMessages(messageID) {
// check DB for messageID and get messageInstanceID
const messageInstanceID = await MessageLink.findOne({ attributes: ['messageInstanceID'], where: { messageID } }).catch(ERR);
if (!messageInstanceID) return null;
// get all messageIDs
const coreID = messageInstanceID.messageInstanceID;
const allMessageIDs = await MessageLink.findAll({ attributes: ['messageID', 'channelID'], where: { messageInstanceID: coreID } }).catch(ERR);
MessageLink.destroy({ where: { messageInstanceID: coreID } });
return allMessageIDs;
}

// Deletes all messages in every channel, if its deleted in one
module.exports.run = async (message) => {
// check if channel is part of service
if (!await BridgedChannel.findOne({ attributes: ['channelID'], where: { channelID: message.channel_id } }).catch(ERR)) return;

const allMessageIDs = await getMessages(message.id);
if (!allMessageIDs) return;

allMessageIDs.forEach(async (entry) => {
if (message.id === entry.messageID) return;
const channel = await client.channels.cache.get(entry.channelID);
// check if bot has permissions to delete messages
if (!channel.guild.members.me.permissionsIn(channel).has(PermissionsBitField.Flags.ManageMessages)) return;
// existence check attempt
if (!await channel.messages.fetch(entry.messageID)) return;
channel.messages.delete(entry.messageID);
});
};
const { PermissionsBitField } = require('discord.js');

const BridgedChannel = require('../../database/models/bridgedChannel');

const MessageLink = require('../../database/models/messageLink');

async function getMessages(messageID) {
// check DB for messageID and get messageInstanceID
const messageInstanceID = await MessageLink.findOne({ attributes: ['messageInstanceID'], where: { messageID } }).catch(ERR);
if (!messageInstanceID) return null;
// get all messageIDs
const coreID = messageInstanceID.messageInstanceID;
const allMessageIDs = await MessageLink.findAll({ attributes: ['messageID', 'channelID'], where: { messageInstanceID: coreID } }).catch(ERR);
MessageLink.destroy({ where: { messageInstanceID: coreID } });
return allMessageIDs;
}

// Deletes all messages in every channel, if its deleted in one
module.exports.run = async (message) => {
// check if channel is part of service
if (!await BridgedChannel.findOne({ attributes: ['channelID'], where: { channelID: message.channel_id } }).catch(ERR)) return;

const allMessageIDs = await getMessages(message.id);
if (!allMessageIDs) return;

allMessageIDs.forEach(async (entry) => {
if (message.id === entry.messageID) return;
const channel = await client.channels.cache.get(entry.channelID);
// check if bot has permissions to delete messages
if (!channel.guild.members.me.permissionsIn(channel).has(PermissionsBitField.Flags.ManageMessages)) return;
// existence check attempt
if (!await channel.messages.fetch(entry.messageID)) return;
channel.messages.delete(entry.messageID);
});
};
8 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Phil
PhilOP3y ago
npm list discord.js
[email protected] /home/phil/Documents/GIT/I-SH
npm list discord.js
[email protected] /home/phil/Documents/GIT/I-SH
Node version is v18.5.0 its a raw package as this is the only way i found that i am able to detect and track deletions from messages sent prior to the bots restart
// trigger on deleted message with raw package
client.on('raw', async (packet) => {
if (packet.t === 'MESSAGE_DELETE' && packet.d.guild_id) {
client.functions.get('EVENT_messageDelete').run(packet.d);
}
});
// trigger on deleted message with raw package
client.on('raw', async (packet) => {
if (packet.t === 'MESSAGE_DELETE' && packet.d.guild_id) {
client.functions.get('EVENT_messageDelete').run(packet.d);
}
});
Syjalo
Syjalo3y ago
There're partials for that
d.js docs
d.js docs3y ago
guide Popular Topics: Partial Structures read more
Syjalo
Syjalo3y ago
Catch the error. You can use try...catch or <Promise>.catch()
Phil
PhilOP3y ago
that didnt work
Syjalo
Syjalo3y ago
Basic JS, Not related to discord.js
Phil
PhilOP3y ago
to be fair, i did mess with it before... i will check it out a catch throws the same error when applied to the delete function ill just look into using the partials and implement it normally.. maybe that way it works? will make sure to update will try seems that its not working fine. the catch was just too empty. if it cant delete a message, its not a big deal anyways. so i just return the catch

Did you find this page helpful?