我试图在!! userinfo命令中显示角色,但出现错误

时间:2019-02-07 15:25:34

标签: javascript discord discord.js

我有一个!!userinfo命令,我试图将其传递到@anyone可以使用的任何地方,并且在那里显示信息,说明我如何执行其他所有操作,但后来我想到了这个问题,这是错误。 / p>

  

UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ map”

我没有找到任何答案,但是我确实想出了一个说它通常表示没有人居住的东西,但是我不知道如何将它放入那里。

const Discord = module.require("discord.js");
const fs = require("fs");
const userdata = JSON.parse(fs.readFileSync('commands/storage/userdata.json', 'utf8'));

module.exports.run = async (bot, message, args) => {
    let member;
    if (message.mentions.users > 0) {
        member = message.mentions.user.size()
    } else {
        member = message.author
    }
    let user;
    if (message.mentions.users > 0) {
        user = message.mentions.user.size()
    } else {
        user = message.author
    }

    embed = new Discord.RichEmbed()
        .setAuthor(message.member.username)
        .setDescription("Users Info", true)
        .setColor("#64FF00", true)
        .addField("Full Username:", `${message.member.username}${message.member.discriminator}`, true)
        .addField("ID:", message.member.id, true)
        .addField("Created at:", message.member.createdAt, true)
        .addField("Status:", `${user.presence.status}`, true)
        .addField("Game:", `${user.presence.game}`, true)
        .addField("Roles", member.roles.map(r => `${r}`).join('|'), true);

    message.channel.send(embed);
}

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

我想要它,这样我可以@anyone并显示信息

3 个答案:

答案 0 :(得分:1)

您可以轻松制作第一部分:

let member;
if (message.mentions.users > 0) {
    member = message.mentions.user.size()
} else {
    member = message.author
}
let user;
if (message.mentions.users > 0) {
    user = message.mentions.user.size()
} else {
    user = message.author
}

进入:

const user = message.mentions.users.first() || message.author;
const member = message.mentions.members.first() || message.member;
if(!member) return message.channel.send('This command can only be run in a guild!')

您还想将embed位更改为:

let embed = new Discord.RichEmbed()
    .setAuthor(user.tag)
    .setDescription("Users Info", true)
    .setColor("#64FF00", true)
    .addField("Full Username:", user.tag , true)
    .addField("ID:", user.id, true)
    .addField("Created at:", user.createdAt, true)
    .addField("Status:", user.presence.status , true)
    .addField("Game:", user.presence.game ? user.presence.game : 'none' , true)
    .addField("Roles", member.roles.map(r => `${r}`).join(' | '), true);
 message.channel.send(embed);

答案 1 :(得分:0)

这会将member设置为数字

member = message.mentions.user.size()

由于member现在是数字,因此尝试访问member.roles的结果为undefined。而且由于undefined没有.map方法,您会看到该异常。

答案 2 :(得分:0)

我认为问题在于如何为变量member赋值。除此之外,我认为您有一些冗余代码,因为您有一个变量member和变量user,它们用相同的代码给出一个值。

在下面,您可以找到我已重写的代码。试试看,让我知道结果是什么。

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

  if (message.mentions.members.first()) {
    guildMember = message.mentions.members.first();
  } else {
    guildMember = message.member;
  }

  // We need the User object aswell for different properties
  const user = guildMember.user;

  let embed = new Discord.RichEmbed()
    .setAuthor(user.username)
    .setDescription("Users Info", true)
    .setColor("#64FF00", true)
    .addField("Full Username:", `${user.username}${user.discriminator}`, true)
    .addField("ID:", user.id, true)
    .addField("Created at:", user.createdAt, true)
    .addField("Status:", `${user.presence.status}`, true)
    .addField("Game:", `${user.presence.game}`, true)
    .addField("Roles", guildMember.roles.map(r => `${r}`).join('|'), true);

  message.channel.send(embed);
}