如何让我的机器人不记录只是嵌入的消息?
How do I get my bot not to log messages that are just embeds?
目前,我正在制作 Discord 机器人并实现了日志记录功能。当我从包含嵌入的 bot 或 webhook 中删除消息时,它会将其记录为已删除的消息,但没有消息内容。所以,我的问题是,如何让我的机器人不记录包含嵌入的已删除消息?
我试着检查消息是否有任何参数,如果有的话 return,但这似乎没有用。
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);
})
你也可以这样做
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 (message.partial)
部分,您将需要确保已定义部分代码:
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
partials: ['MESSAGE']
})
目前,我正在制作 Discord 机器人并实现了日志记录功能。当我从包含嵌入的 bot 或 webhook 中删除消息时,它会将其记录为已删除的消息,但没有消息内容。所以,我的问题是,如何让我的机器人不记录包含嵌入的已删除消息?
我试着检查消息是否有任何参数,如果有的话 return,但这似乎没有用。
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);
})
你也可以这样做
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 (message.partial)
部分,您将需要确保已定义部分代码:
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
partials: ['MESSAGE']
})