首页 > 解决方案 > 我的不和谐机器人的成员命令 - discord.js

问题描述

所以我一直在尝试创建一个 ?members 命令来列出所有具有角色的用户。

到目前为止,我有这个:

if (message.content.startsWith("?members")) {
        let roleName = message.content.split(" ").slice(1).join(" ");

        let membersWithRole = message.guild.members.filter(member => {
            return member.roles.find("name", roleName);
        }).map(member => {
            return member.user.username;
        })

        const embed = new Discord.RichEmbed({
            "title": `Members in ${roleName}`,
            "description": membersWithRole.join("\n"),
            "color": 0xFFFF

        });

        return message.channel.send(embed);



    }

因此,如果您键入角色的确切名称,它会起作用,但在您 ping 或键入第一个单词时不起作用。我已经尝试了几个小时来弄清楚如何做到这一点,我想我应该寻求帮助。

提前致谢!

标签: discord.js

解决方案


Pings get translated into a code as they come through, there is a lot of information on how to parse them in the official guide After it's parsed into a role id you can just use members.roles.get() because that is what they are indexed by.

As for finding a partial name, for that you are going to have to run a function on your find and use String.includes.

return member.roles.find(role => role.name.includes(roleName));

This will also work for find the whole name of course, so it can replace your existing line.

However, this may result in more than one role. This is also true for searching by the entire role name, however, as there are no restrictions on duplicate named roles. For this you may want to invert you design and search through message.guild.roles first for any matching roles, then search for members with the roles found.


推荐阅读