javascript - 使用 message.mentions.channels 提及 channel

标签 javascript node.js discord discord.js

所以我只需要一些关于如何在 Discord.js 上做某事的帮助。

我对如何使用 message.mentions.channels 感到非常困惑,并且文档没有帮助。

我只需要一个 console.log channel 提到的命令(如 #annoucements),不知道如何执行。我现在的代码是

if (args.join(" ").mentions.channels = true) {
    console.log(args.join(" ").mentions.channels)

这是使用的命令-!setWelcome #announcementsargs 去掉前缀/命令并留下 #announcements

但它返回Cannot set property 'channels' of undefined.

如何简单?我很困惑。

如何才能使用 -!setWelcome #announcements 记录 #announcements

(我还在学习中,请原谅这里缺乏 JavaScript 技能)

最佳答案

首先,您的 if 语句需要 2 或 3 个 = 字符来检查 args.join("").mentions.channel 是否等于 true。像这样的事情:

if (args.join(" ").mentions.channels === true) {
    // ... the rest of your code

其次,您可以通过多种方式控制台记录消息中提及的所有 channel 。您可以通过以下两种方法来实现:

方法 1:记录保存所有 channel 的Collection。这很有用,但在控制台中可能会让人不知所措,因为Collection内部包含大量属性和函数。

function logMentionChannels(message) {
    let channels = message.mentions.channels;

    console.log(channels);
}

方法 2:使用 ...channels.map() 仅将所需的 channel 属性提取到对象中。

function logMentionChannels(message) {
    let myChannels = channels.map(channel => new Object({
        'id': channel.id,
        'name': channel.name
    }));

    console.log(myChannels);
}

如果您需要其他方式循环提及 channel ,请参阅 Collection 上的文档。

关于javascript - 使用 message.mentions.channels 提及 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48604671/

相关文章:

javascript - jquery stopPropagation 停止 ajax get

javascript - 将自定义 header 添加到 videojs 播放器源

node.js - 在 Angular 通用应用程序(nodejs)上集成 newrelic

javascript - 如何使用 discord.js 创建角色

python - 为什么多个 on_message 事件不起作用?

javascript - 如何在 Haskell 中编写这个多元组合函数?

javascript - 变量在声明之前如何使用?

linux - 进程间通信

node.js - 使用 $lookup 比较两个字段的查询

javascript - DM发送成功时回复