检查消息是否是 DM。 (Discord.js 和 discord.js-突击队)

Checking if a message is a DM. (Discord.js and discord.js-commando)

如何检查消息是否是 Discord.js 中的 DM?我已经尝试了几种方法来对此进行测试,我已经尝试了这些方法:

if (msg.channel.isDM) ... Produced undefined
if (msg.isDM) ... Produced undefined
if (msg.channel.DMChannel.isDM) ... produced undefined
if (msg.DMChannel) ... produced undefined
if (msg.channel.DMChannel.dm) ... produced undefined
if (msg.channel.dm) ... produced undefined

在控制台中生成 msg 后,我尝试了所有方法,并试图破译我到底在哪里做的。甚至文档也不是很清楚在哪里检查这个。我看过多个刚刚喷出 <message>.channel.dm 的“解决方案”。嗯……那不行。

编辑: 所以它看起来确实是 <message>.isDM。但是,在我的命令中这不起作用。我可以在数组中找到 isDM,当我 console.log(msg) 时,它产生了预期的 true。但是,执行 console.log(msg.isDM) 会打印出 undefined。这看起来很奇怪,因为它显然是定义的。

我找到了几种方法来处理这个问题:

<message>.channel.type === 'dm' 然而这只包括单个 DM,但将允许组等

message.guild === null 将检查所有 DM(如果消息服务器为 null,如果消息不是通过服务器发送的,则 return 为真,则必须是 dm。)

DMChannel 是 class,所以 message.channel instanceof DMChannel 应该可以。

client.GetDMChannelAsync(msg.Channel.Id).Result != null

这就是您在最新版本中检查的方式

client.on("message", msg =>  { 
    
   if(msg.guild==null &&msg.author.id!=='botDiscordId'){
    msg.reply('dosomethinghere')
   }
});

这使用公会 class 并将回复用户消息,您可以将 msg.reply 更改为任何内容,我建议将 botDiscordId 放在字段中,这样机器人就不会'无法识别自己的消息。