Discord.js V12 Userinfo createdAt / joinAt未定义

时间:2020-05-20 05:56:04

标签: undefined discord.js userinfo

这是我的新手,尝试去这里学习,但是我在这段代码中苦苦挣扎,完全感到困惑。 iv设法使一切正常(最终),但以下问题除外,.userinfo @randomUser生成除

以外的所有用户详细信息

*注册日期 和 *加入日期

删除这些行可以使其他所有内容正常运行。还可以显示用户角色吗?

  • 错误

    userinfo.registered = moment.gmt(m.guild.members.fetch(user.id).user.createdAt); ^

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

const Discord = require("discord.js");
const moment = require("moment");

const bot = new Discord.Client({ disableEveryone: true});

bot.on("ready", () => console.log(`Connected to Discord!`))

bot.on("message", m => {
    let prefix = ".";
    if (m.author.bot || !m.guild) return;
    if (!m.content.startsWith(prefix)) return;

if (m.content.startsWith(prefix + "userinfo")) {
    let user = (m.mentions.users.first()) || m.author;
    let member = m.mentions.members.first() || m.member;


    let userinfo = {};
    userinfo.avatar = user.displayAvatarURL()
    userinfo.name = user.username;
    userinfo.discrim = `#${user.discriminator}`;
    userinfo.id = user.id;
    userinfo.status = user.presence.status;
    userinfo.registered = moment.gmt(m.guild.members.fetch(user.id).user.createdAt);
    userinfo.joined = moment.gmt(m.guild.members.fetch(user.id).joinedAt);

    const embed = new Discord.MessageEmbed()
    .setAuthor(user.tag, userinfo.avatar)
    .setThumbnail(userinfo.avatar)
    .addField(`Username`, userinfo.name, true)
    .addField(`Discriminator`, userinfo.discrim, true)
    .addField(`Status`, userinfo.status, true)
    .addField(`ID`, userinfo.id)
    .addField(`Registered`, userinfo.registered)
    .addField(`Joined`, userinfo.joined)

    return m.channel.send(embed);
}



});

bot.login("XXX")

1 个答案:

答案 0 :(得分:0)

.fetch是一个异步函数,因此您需要使用await.then,您已经拥有一个Userlet user = (m.mentions.users.first()) || m.author的实例,为什么不这样做用那个?

userinfo.registered = moment.gmt(user.createdAt);
userinfo.joined = moment.gmt(user.joinedAt);