当我向我的机器人发送 DM 时,事件 messageCreate 没有触发/发射(discord.js v13)
我编写了这段代码来记录发送到我的机器人的 DM:
I made this code for logging the DMs that are sent to my bot:
client.on('messageCreate', async message => {
if (message.author.bot) return;
const attachment = message.attachments.first()
if (message.channel.type === 'DM') {
console.log(message.content)
const dmLogEmbed = new MessageEmbed()
.setColor("RANDOM")
.setTitle(`${message.author.tag} dmed the bot and said: `)
.setDescription(message.content)
.setFooter(`User's id: ${message.author.id}`)
if (message.attachments.size !== 0) {
dmLogEmbed.setImage(attachment.url)
}
client.channels.fetch("852220016249798756").then((channel) => {
channel.send({ embeds: [dmLogEmbed] })
})
}
});
但是当更新到 discord.js v13 时它不再起作用了,因为我知道唯一的变化是dm"频道类型不再是dm"而是DM",所以我将其更改为我的代码,但它仍然无法正常工作,我真的不知道为什么.
But when updating to discord.js v13 it didn't work anymore, for what I understood the only change is that the 'dm' channel type isn't 'dm' anymore but 'DM', so I changed it in my code, but it is still not working and I don't really know why.
推荐答案
确保您的客户意图中有 DIRECT_MESSAGES
.
Make sure you have DIRECT_MESSAGES
in your client's intents.
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"] });
Discord API v8 中有一个重大更改.如需参考,请参阅此处.
There is a breaking change in the Discord API v8. For reference see here.
在 Discord API v8 及更高版本中,DM 频道不会发出 CHANNEL_CREATE
事件,这意味着 discord.js 无法自动缓存它们.为了让您的机器人接收 DM,必须启用 CHANNEL
部分.
On Discord API v8 and later, DM Channels do not emit the
CHANNEL_CREATE
event, which means discord.js is unable to cache them automatically. In order for your bot to receive DMs, theCHANNEL
partial must be enabled.
所以我们也需要启用部分CHANNEL
.请记住,在处理部分数据时,您通常希望获取 数据,因为它不完整.但是,如果您使用的是 messageCreate
事件,这似乎不是您的情况.收到的 message
不是部分的,也不是 message.channel
.要检查是否有部分内容,您可以使用 .partial
属性.例如 Message.partial
或 Channel.partial
.
So we need to enable the partial CHANNEL
as well. Keep in mind that when dealing with partial data, you often want to fetch the data, because it is not complete. However this doesn't seem to be your case, if you are using the messageCreate
event. The message
received is not partial nor message.channel
. To check if something is partial, you can use the .partial
property. For example Message.partial
or Channel.partial
.
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"], partials: ["CHANNEL"] });
现在它应该可以像旧的 discord.js v12 一样工作了.
Now it should work as good old discord.js v12.
相关文章