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

查看:68
本文介绍了遍历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?

推荐答案

我不完全了解您的角色,但我想您是在谈论公会角色。您可以从消息中获取公会,然后再获取它的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();




正如您所说,您不确定要做什么,但是如果这就像一个'将用户分配给角色'命令,则可以这样做:


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);
});

有用的链接:

消息#guild | discord.js

Guild#roles | discord.js

RoleManager#cache | discord.js

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

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