'How do I get my bot not to log messages that are just embeds?
Currently, I'm making a Discord bot and I implemented a logging feature. When I delete a message from a bot or webhook that contains an embed, it logs it as a deleted message, but with no message content. So, my question is, how do I get my bot not to log deleted messages that contain an embed?
I tried checking if the message had any arguments, and if so to return, but that didn't seem to work.
Solution 1:[1]
You could also do something like this
client.on('messageDelete', async message => {
const logChannel = client.channels.cache.get(messageLog)
if (message.author.bot) return
// You can choose to remove this line if you want the if/else statement below to catch the message
if (message.partial || !message.author) {
// logs if the message that is deleted was sent before the bot came online/restarted or if the message contains no author information
logChannel.send({
content: `A message was deleted in the <#${message.channel.id}> channel. It's content is unknown.`
})
} else {
// logs if the message that is deleted was sent after the bot came online/restarted
const messageContent = message.content || 'No content, likely an embed.' // Captures the content or if no content, states as such.
logChannel.send({
content: `The message by ${message.author} shown below was deleted from <#${message.channel.id}>:\n\n${messageContent}`,
split: true // split the message in case it contains more than 2000 characters
})
}
})
If you chose to use the if (message.partial) part of that code, you will need to make sure you have partials defined:
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
partials: ['MESSAGE']
})
Solution 2:[2]
client.on('messageDelete', (message) => {
// check if there is actually content
if (message.content) console.log(message.content);
// or you can just check if there are no embeds (both work):
if (!message.embeds[0]) console.log(message.content);
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gh0st |
| Solution 2 | Harry Allen |
