我没有在 discord.js npmjs 插件上获取 Dms console.log

I m not getting Dms console.log on discord.js npmjs plugin

"messageCreate" 事件中的 console.log() 在发送 DM 时未触发。


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

Client.on('messageCreate', (msg) => {
   msgArray = msg.content.toLowerCase().split(' ')

   if (msgArray[0] == "/botmsg"){
    Client.users.fetch(msgArray[1], false).then((user) => {
        user.send(msgArray[2]);
       });
   } else {
    console.log(msg.content);
   }
})

Client.login('token hidden')

我的目标是在收到 DM 时记录消息,我该怎么做?

您应该将 console.log 函数放在代码的“真实”部分。

if (msgArray[0] == "/botmsg"){
    Client.users.fetch(msgArray[1], false).then((user) => {
        user.send(msgArray[2]);
    });
    console.log(msg.content);
} else {
    console.log(msg.content);
}

使用 discord.js v13 您需要启用部分 CHANNEL,因此您的客户端必须

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