如何在 Discord.js 中检查消息是否为 DM?

How do I check if a message is a DM in Discord.js?(如何在 Discord.js 中检查消息是否为 DM?)
本文介绍了如何在 Discord.js 中检查消息是否为 DM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

目前我使用的是最新版本的 Discord.js (v13.1.0).

Currently I am using the latest version of Discord.js (v13.1.0).

我希望能够检测某条消息是否是 DM.我尝试制作 messageCreate 事件并执行 if 语句来检查频道是否为 DM

I want to be able to detect if a certain message is a DM. I tried making a messageCreate event and did an if statement to check if the channel is a DM

client.on('messageCreate', message => {
   if (message.channel.type == 'DM') {
      console.log('Dm recieved!')
   }
})

由于某种原因,这不起作用.所以我不知道会发生什么.

This for some reason didn't work. So I have no idea what will.

这是我的意图:

[Intents.FLAGS.GUILDS、Intents.FLAGS.GUILD_MESSAGES、Intents.FLAGS.GUILD_MESSAGE_REACTIONS、Intents.FLAGS.DIRECT_MESSAGES]

顺便说一句,事件确实起作用了:

Btw, the event does work:

client.on('messageCreate', message => {
   console.log('Event')
}

推荐答案

我之前也遇到过这个问题;我深入研究了 discord.js v12 和 v13 之间的变化并找出了原因.

I had this issue earlier as well; I dug into the changes between discord.js v12 and v13 and figured out why.

如 这里在 href="https://discordjs.guide" rel="nofollow noreferrer">discordjs.guide 专门介绍更新之间的重大更改的网页:

As seen here on the discordjs.guide webpage dedicated to the breaking changes between the updates:

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

对于处理 DM 消息的非常重要的功能,该通知很难找到.它只直接提到了 CHANNEL_CREATE 事件,但基本上它的意思是您的机器人 无法 根本无法接收 DM,除非 CHANNEL 部分是在您的客户端选项中启用.

For the very important feature of handling DM messages, that notice was pretty difficult to find. It only directly mentions the CHANNEL_CREATE event, but basically what it means is that your bot is unable to receive DMs at all unless the CHANNEL partial is enabled in your client options.

以下是您将如何做到这一点的示例:

Here is an example of how you would do that:

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

您需要将该 partials 属性添加到您的 Client 构造函数中才能接收 DM.这样做后,您的代码将正常工作.

You need to add that partials property to your Client constructor in order to receive DMs. After doing so, your code will work properly.

这篇关于如何在 Discord.js 中检查消息是否为 DM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)