简体   繁体   English

获取提及用户的角色

[英]Getting roles of mentioned user

Working on a marriage system for my Discord bot but I am struggling to find out of a user has the "married" role already.为我的 Discord 机器人开发婚姻系统,但我很难找出用户已经拥有“已婚”角色。

    const Discord = require('discord.js');
module.exports = {
    name: "marry",
    aliases: ['m'],
    description: "marry someone",


    async run(client, message, args) {

        const user = message.mentions.users.first() || client.users.get(args[0]);
        let marriedRole = message.guild.roles.cache.find(r => r.name === "Married");
        let singleRole = message.guild.roles.cache.find(r => r.name === "Single");

        let proposerID = message.author.id;
        let proposerName = message.author.username;
        if (user.roles.cache.some(marriedRole)) {
            let embed = new Discord.MessageEmbed()
            .setDescription(`Sorry **${user.tag}**, is already married!`);
            let messageEmbed = await message.channel.send({ embeds: [embed] });
        }
        if (message.member.roles.cache.some(role => role.name === 'Single')) {
            let embed = new Discord.MessageEmbed()
            .setDescription(`**${user.username}**, **${message.author.username}** is asking for your hand in marriage, would you like to accept?`);
            let messageEmbed = await message.channel.send({ embeds: [embed] });
            messageEmbed.react('✅');
            messageEmbed.react('❌');
        }
        if (message.member.roles.cache.some(role => role.name === 'Married')) {
            let embed = new Discord.MessageEmbed()
            .setDescription(`Sorry **${message.author.username}**, but you are already married!`);
            let messageEmbed = await message.channel.send({ embeds: [embed] });
        }

        }
}

I keep getting the error我不断收到错误

if (user.roles.cache.some(marriedRole)) { ^ if (user.roles.cache.some(marriedRole)) { ^

TypeError: Cannot read properties of undefined (reading 'cache') TypeError:无法读取未定义的属性(读取“缓存”)

By using message.mentions.users.first() , you are getting a User object in return which doesn't have the roles property.通过使用message.mentions.users.first() ,您将获得一个不具有roles属性的User对象作为回报。 Instead, you need the GuildMember object.相反,您需要GuildMember对象。 To get it, all you have to do is change the part where you declare the user variable to:要获得它,您所要做的就是将声明user变量的部分更改为:

const user = message.mentions.members.first() || client.users.get(args[0]);

Answer from Zsolt Meszaros Since, if there is no mention in the message, message.mentions.members.first() , so keep that in mind. Zsolt Meszaros 的回答因为,如果消息中没有提及message.mentions.members.first() ,请记住这一点。 client.users.get(args[0]) would also just get a User object, so instead of using that, you can use this: client.users.get(args[0])也只会获得一个User对象,因此您可以使用它而不是使用它:

const user = message.guild.members.cache.get(args[0])

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

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