Event messageCreate not firing/emitting when I send a DM to my bot (discord.js v13)(当我向我的机器人发送 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.
这篇关于当我向我的机器人发送 DM 时,事件 messageCreate 没有触发/发射(discord.js v13)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当我向我的机器人发送 DM 时,事件 messageCreate 没


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01