【问题标题】:Cannot read property username of undefinded discord.js无法读取未定义 discord.js 的属性用户名
【发布时间】:2021-06-15 00:48:48
【问题描述】:

我正在发出船舶命令,错误消息有效,但运输无效。我总是收到这个错误:Type Error: Cannot read property "username" of undefined。有人可以帮我解决吗? 这是我的代码:

const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
    name: 'ship',
    description: "Ship two users!",
    execute(message, args) {
    const user = message.mentions.members.first();
    const user2 = message.mentions.members.first(+1) ;
    const images = [(1-100) code is different, put every number there]
    const result = Math.floor(Math.random() * images.length);
    const error = new Discord.MessageEmbed()
    .setColor("#000000")
    .setTitle("Ship")
    .setDescription("There was an error: Check if you mentioned a user other than you!")
    .setFooter(message.author.username)
    .setTimestamp()
    ;

    if(!user) return message.reply(error);
    if(!user2) user2 = message.author.username;
    if(user.id === message.author.id) return message.reply(error);

    const embed = new Discord.MessageEmbed()
    .setColor("#2A333E")
    .setTitle("Ship")
    .setDescription(user2.user.username + " + " + user.user.username + " =")
    .addField(images[result] + "%" , "Is this good or not so good?")
    .setFooter(message.author.username)
    .setTimestamp()
    ;

    message.channel.send(embed)
    }}

【问题讨论】:

  • 你能提供完整的堆栈跟踪吗?这将帮助我们确定我们需要关注哪条线
  • 我已经看过了,是哪一行。它在 embed const 描述中,所以第 28 行。

标签: node.js discord discord.js bots


【解决方案1】:

if(!user2) user2 = message.author.username;

这是有问题的行 - 您将 user2 的值设置为等于作者的用户名。请尝试将其设置为仅等于 message.author

此外,您正在尝试从不存在的 User 对象中检索 .user 属性(因为它已经是 User 对象。)您需要更改 user2.user.usernameuser.user.username只需 user2.usernameuser.username

【讨论】:

  • 首先感谢您的帮助!我现在不再收到错误,但嵌入中的用户仅显示为未定义。
猜你喜欢
  • 2021-06-26
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 2022-06-10
  • 2021-04-12
  • 2021-06-27
相关资源
最近更新 更多