简体   繁体   English

如何检查用户是否有任何角色 discord.js

[英]How to check if an user has any role discord.js

I'm making a user info command in my discord bot using discord.js v12.我正在使用 discord.js v12 在我的 discord bot 中创建用户信息命令。 The only problem I have with it is, that if an user doesn't have any roles, the highest role shows as @@everyone, not @everyone.我遇到的唯一问题是,如果用户没有任何角色,则最高角色显示为@@everyone,而不是@everyone。 So I would like to avoid it, and make it so if the user doesn't have any roles, it will say 'This user has no roles in this server'.所以我想避免它,如果用户没有任何角色,它会说'这个用户在这个服务器中没有角色'。 My code is here (only the bit that has anything to do with the roles):我的代码在这里(只有与角色有关的部分):

const { DiscordAPIError } = require("discord.js");
const Discord = require("discord.js");
const moment = require("moment");

module.exports = {
    name: "profile",
    description: "The bot will return the info about the user",
    execute(message, args){
        let userinfoget = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member

var highestRoleID = userinfoget.roles.highest.id;
        var joined = moment(userinfoget.joinedAt).format('DD/MM/YY, HH:mm:ss')
        console.log(`Highest role = ${highestRoleID}`);

        console.log(`User = ${userinfoget}`);
        console.log(userinfoget.roles)
        if(!userinfoget.roles) console.log('no roles')

const embed = new Discord.MessageEmbed()

        .setColor(userinfoget.displayHexColor)
        .setAuthor(`${userinfoget.user.tag}`, userinfoget.user.displayAvatarURL())
        .addFields(
            {name: `User ping`,
            value: `<@${userinfoget.id}>`}
        )
        .addFields(
            {name: `User ID`,
            value: `${userinfoget.id}`}
        )
        .addFields(
            {name: 'Joined Server',
            value: moment(userinfoget.joinedAt).format('LLLL') + ' ' + timeFromJoiningServerMessage} // or moment(userinfoget.joinedAt).format('DD/MM/YY, HH:mm:ss')
        )
        .addFields(
            {name: 'Joined Discord',
            value: moment(userinfoget.user.createdAt).format('LLLL')} // or moment(userinfoget.createdAt).format('DD/MM/YY, HH:mm:ss')
        )
        .addFields(
            {name: 'Highest role',
            value: `<@&${highestRoleID}>`}
        )
        .addFields(
            {name: 'Online Status',
            value: `${status}`}
        )
        .addFields(
            {name: 'Is a bot?',
            value: `${isBot}`}
        )
        .setFooter('Bot made by mkpanda')
        message.channel.send(embed);
}

As you can see, I tried doing !userinfoget.roles , but when logging it, it shows all the information about the user, not just the roles.如您所见,我尝试执行!userinfoget.roles ,但是在记录它时,它显示了有关用户的所有信息,而不仅仅是角色。 How could I detect whether an user has any role or not?如何检测用户是否具有任何角色? Thanks in advance :)提前致谢 :)

GuildMember.roles.cache is a Collection (very similar to an Array ). GuildMember.roles.cache是一个Collection (非常类似于一个Array )。 Therefore, you can use Collection#filter and check if the Role 's name equals @everyone .因此,您可以使用Collection#filter并检查Role的名称是否等于@everyone That'll remove the @everyone role from the Collection and you can check if the GuildMember has any roles.这将从Collection删除@everyone角色,您可以检查GuildMember是否有任何角色。


.addFields({
    name: 'Highest role',
    value: message.member.roles.cache.filter(role => role.name !== "@everyone").size > 0 ? message.member.roles.highest : "This member has no roles."
})

Useful links I strongly recommend you to visit:我强烈建议您访问有用的链接:

Collection#map 收藏#地图

Collection#filter 收藏#过滤器

Conditional (ternary) operator 条件(三元)运算符

You could use this ;你可以用这个;

userinfoget.roles.cache.size === 1 ? 'No roles' : userinfoget.roles.cache.map(r => '<@&' + r.id + '>').join(', ')

Since if no user has no roles then they will only be "@everyone" so the role size would be one.因为如果没有用户没有角色,那么他们只会是“@everyone”,所以角色大小将为一。 You can use this information to see if the user has or not roles您可以使用此信息查看用户是否具有角色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM