如何显示用户 discord.js/userinfo 命令的角色

2022-01-10 00:00:00 discord bots javascript discord.js

我正在尝试创建一个 userinfo 命令,但目前我一直在显示用户的角色.

I'm trying to make a userinfo command, and I'm currently stuck on showing roles of the user.

这是我的代码:

const Discord = module.require('discord.js');
const moment = require('moment');

module.exports.run = async (bot, message, args) => {

    let user = message.mentions.users.first() || message.author;
    const joinDiscord = moment(user.createdAt).format('llll');
    const joinServer = moment(user.joinedAt).format('llll');
    let embed = new Discord.RichEmbed()
        .setAuthor(user.username + '#' + user.discriminator, user.displayAvatarURL)
        .setDescription(`${user}`)
        .setColor(`RANDOM`)
        .setThumbnail(`${user.displayAvatarURL}`)
        .addField('Joined at:', `${moment.utc(user.joinedAt).format('dddd, MMMM Do YYYY, HH:mm:ss')}`, true)
        .addField('Status:', user.presence.status, true)
        .addField('Roles:', user.roles.map(r => `${r}`).join(' | '), true)
        .setFooter(`ID: ${user.id}`)
        .setTimestamp();

    message.channel.send({ embed: embed });
    return;
}

module.exports.help = {
    name: 'userinfo'
}

我收到此错误TypeError: Cannot read property 'map' of undefined,我不知道如何解决?

I'm getting this error TypeError: Cannot read property 'map' of undefined and I don't know how to fix it?

推荐答案

User.rolesundefined 因为该属性不存在:尝试使用 GuildMember.roles:

User.roles is undefined because that property doesn't exist: try using GuildMember.roles instead:

let member = message.mentions.members.first() || message.member,
  user = member.user;

let embed = new Discord.RichEmbed()
  // ... all the other stuff ...
  .addField('Roles:', member.roles.map(r => `${r}`).join(' | '), true)

其他属性仍会使用user,但.roles会与GuildMember相关.

The other properties will still use user, but .roles will be related to the GuildMember.

相关文章