【问题标题】:Event messageCreate not firing/emitting when I send a DM to my bot (discord.js v13)当我向我的机器人发送 DM 时,事件 messageCreate 没有触发/发射(discord.js v13)
【发布时间】:2021-10-12 10:49:45
【问题描述】:

我编写了这段代码来记录发送到我的机器人的 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”频道类型不再是“dm”而是“DM”,所以我将其更改为我的代码,但它仍然无法正常工作,我真的不知道为什么。

【问题讨论】:

  • 你调试了什么?确认您的 messageCreate 事件正在发出并检查 if 语句是否签出
  • 我已成功重现您的问题,但不知道为什么会发生。我认为这是因为您缺少意图DIRECT_MESSAGES。我补充说,但仍然没有...messageCreate 没有完全触发...(在行会中工作)
  • 是的,我也认为问题出在事件中,也许 dms 发出这些类型事件的方式发生了变化,但我不确定,我也不知道
  • 以某种方式工作,但我不确定它是否是您想要的。机器人必须首先向用户发送至少一个 DM。然后messageCreate 照常开火。不确定是否有办法解决这个问题。我会调查的。
  • 我已经在我的回答中为你的问题写了一个完整的解决方案,你可以去看看。

标签: javascript node.js discord discord.js


【解决方案1】:

确保您的客户意图中有 DIRECT_MESSAGES

const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"] });

Discord API v8 中有一个重大更改。参考见here

在 Discord API v8 及更高版本中,DM 通道不会发出 CHANNEL_CREATE 事件,这意味着 discord.js 无法自动缓存它们。为了让您的机器人接收 DM,必须启用 CHANNEL 部分。

所以我们也需要启用部分CHANNEL。请记住,在处理partial data 时,您通常希望获取 数据,因为它不完整。但是,如果您使用的是 messageCreate 事件,这似乎不是您的情况。收到的message 不是部分的,也不是message.channel。要检查某些内容是否不完整,您可以使用 .partial 属性。例如Message.partialChannel.partial

const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"], partials: ["CHANNEL"] });

现在它应该可以像旧的 discord.js v12 一样工作了。

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 2021-06-13
    • 2021-03-03
    • 2022-01-09
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多