問題描述
我編寫了這段代碼來記錄發送到我的機器人的 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.
這篇關于當我向我的機器人發送 DM 時,事件 messageCreate 沒有觸發/發射(discord.js v13)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!