在 discord.js 中迭代成员的角色时出错 [英] Error when iterating through a member's roles in discord.js

查看:16
本文介绍了在 discord.js 中迭代成员的角色时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在为我的不和谐机器人创建一个黑名单功能,你基本上只是输入一个词和一个角色,如果有人扮演那个角色,那么......会发生什么事情吗?还不确定.但是当我尝试遍历成员的角色时,我得到了这个错误:

So i'm making a blacklist function for my discord bot, where you basically just put a word and a role, and if someone with that role then... something will happen? Not really sure yet. But when i try to iterate through a member's roles, I get this error:

(node:21803) UnhandledPromiseRejectionWarning: TypeError: message.member.roles is not iterable

这是我的代码:

for (const word of message.content.split(" ")) {
        for (const role of message.member.roles) {
            console.log(role.name);
        }
    }

关于如何解决这个问题的任何想法?

Any ideas on how to fix this?

推荐答案

我不太明白你的意思是什么,但我想你说的是公会角色.您可以从 Message 中获取 Guild,然后是 RoleManager,因此您可以映射其缓存以获取其中的所有角色.这就是你的做法:

I don't exactly understand what roles you mean, but I suppose you're talking about the Guild Roles. You can get the Guild from the Message, and then it's RoleManager, so you can map its cache to get all the roles inside of it. This is how you do it:

const roles = message.guild.roles.cache.map(role => role);

与 Javascript 中的任何其他数组一样,您可以使用 forEach 循环对其进行迭代.

As any other array in Javascript, you can in iterate through it with a forEach loop.

// This log every role in the Guild the message was sent
roles.forEach(role => {
  console.log(role);
});

对于提到的用户,您可以通过 MessageMentions 对象获取它.像这样:

For the user mentioned you can get it through the MessageMentions object. Like that:

// In case you want the User Object
const user = message.mentions.users.first();
// In case you want the GuildMember object (most likely)
const member = message.mentions.members.first();

有了它,我认为你可以处理你想要达到的目标.无论如何,如果您谈论的是消息中提到的角色,您可以像用户一样获取它(来自 MessageMentions 对象).

With that, I think you can work with what you're trying to reach. Whatever, if you're talking about a role that was mentioned in the message, you can get it just like the user (from the MessageMentions object).

const role = message.mentions.roles.first();

<小时>

正如你所说,你不确定你要做什么,但如果它就像一个'assign user to role'命令,你可以这样做:


As you said, you're not sure about what you're going to do, but if it was like an 'assign user to role' command, you could do it like that:

const member = message.mentions.members.first();
const role = message.mentions.roles.first();

member.roles.add(role);

// !command @user @role - adds the user to the role

或者,如果整个过程中,您只是试图迭代成员的角色.它或多或少类似于第一个示例.给你:

Or if this whole time, you were just trying to iterate through the member's role. It works more or less like the first example. There you go:

// In case you want the mentioned user
const member = message.mentions.members.first();
// In case you want the message author user
const member = message.member;

// Map the User Roles
const roles = member.roles.cache.map(role => role);

// Iterate though it =)
roles.forEach(role => {
  console.log(role);
});

有用的链接:
Message#guild |discord.js
Guild#roles |discord.js
RoleManager#cache |discord.js

这篇关于在 discord.js 中迭代成员的角色时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆