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

How to show roles of user discord.js / userinfo command(如何显示用户 discord.js/userinfo 命令的角色)
本文介绍了如何显示用户 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 命令的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Using discord.js to detect image and respond(使用 discord.js 检测图像并响应)
Check if user ID exists in Discord server(检查 Discord 服务器中是否存在用户 ID)
Guild Member Add does not work (discordjs)(公会成员添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何为我的 Discord.js 机器人编写事件/命令处理程序?)
How to find a User ID from a Username in Discord.js?(如何从 Discord.js 中的用户名中查找用户 ID?)