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

时间:2022-11-06
本文介绍了如何显示用户 discord.js/userinfo 命令的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试创建一个 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.

这篇关于如何显示用户 discord.js/userinfo 命令的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:当我向我的机器人发送 DM 时,事件 messageCreate 没有触发/发射(discord.js v13) 下一条:你如何在 discord.js 中制作嵌入页面

相关文章

最新文章