javascript - 当我向我的机器人发送 DM 时,事件 messageCreate 没有触发/发射(discord.js v13)

标签 javascript node.js discord discord.js

我制作了这段代码来记录发送到我的机器人的 DM:

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” channel 类型不再是“dm”而是“DM”,所以我在我的代码中更改了它,但它仍然无法正常工作,我真的不知道为什么。

最佳答案

确保您拥有 DIRECT_MESSAGES在您的客户的意图。

const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"] });
Discord API v8 中有一个重大变化。引用见here .

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, the CHANNEL partial must be enabled.


所以我们需要启用部分CHANNEL也是。请记住,在处理 partial data 时,你经常想获取 数据,因为它不完整。但是,如果您使用 messageCreate,这似乎不是您的情况。事件。 message收到的不是部分的,也不是message.channel .要检查某些内容是否不完整,您可以使用 .partial属性(property)。例如 Message.partial Channel.partial .
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"], partials: ["CHANNEL"] });
现在它应该像旧的 discord.js v12 一样工作。

关于javascript - 当我向我的机器人发送 DM 时,事件 messageCreate 没有触发/发射(discord.js v13),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68700270/

相关文章:

javascript - 在另一个 JavaScript 应用程序中运行 Node 应用程序

node.js - 在 Ubuntu 14.04 上安装 Node 5.x 和最新或正在运行的 npm

python - 多字 discord 斜杠命令 (PyCord)

javascript - 如何在 Javascript 中去除破折号分隔字符串的最后一个元素

javascript - 追加后元素的大小不更新

javascript - 如何从 Jest 测试套件中排除 CSS 模块文件?

javascript - react 等待服务器响应

node.js - 使用 socket.io-redis 和 RedisToGo 在 Heroku 上扩展到 2+ dynos

javascript - Discord.js 提及,用户未定义

javascript - 使用 Javascript 从关键字搜索返回 Youtube 视频 URL