Discord.js - 当命令没有别名时,帮助命令返回“未定义”

时间:2021-01-15 18:26:13

标签: javascript node.js discord.js

我的帮助命令返回有关命令的信息,当命令没有别名或用法属性时返回 ... 'smtp' => [ 'transport' => 'smtp', 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), 'port' => env('MAIL_PORT', 587), 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'timeout' => null, 'auth_mode' => null, 'from' => [ 'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'), 'name' => env('MAIL_FROM_NAME', 'Example'), ], ], 'info' => [ 'transport' => 'smtp', 'host' => env('INFO_MAIL_HOST', 'smtp.mailgun.org'), 'port' => env('INFO_MAIL_PORT', 587), 'encryption' => env('INFO_MAIL_ENCRYPTION', 'tls'), 'username' => env('INFO_MAIL_USERNAME'), 'password' => env('INFO_MAIL_PASSWORD'), 'timeout' => null, 'auth_mode' => null, 'from' => [ 'address' => env('INFO_MAIL_FROM_ADDRESS', 'hello@example.com'), 'name' => env('MAIL_FROM_NAME', 'Example'), ], ],

我的帮助命令的代码:

undefined

我尝试添加 const helpcommand = new Discord.MessageEmbed() .addField("Command name", `${command.name}`) .addField("Description", `${command.description}`) .addField("Aliases", `${command.aliases}`) .addField("Usage", `${command.usage}`) .setTimestamp() .setFooter(message.member.user.tag, message.author.avatarURL()); message.channel.send(helpcommand) 以使其在没有别名或用法时返回 || 但它不起作用:

none

谁能告诉我如何在没有可用属性的情况下显示 const helpcommand = new Discord.MessageEmbed() .addField("Command name", `${command.name}`) .addField("Description", `${command.description}`) .addField("Aliases", `${command.aliases}` || "none") .addField("Usage", `${command.usage}` || "none") .setTimestamp() .setFooter(message.member.user.tag, message.author.avatarURL()); message.channel.send(helpcommand)

1 个答案:

答案 0 :(得分:0)

Template literals 是允许嵌入表达式的字符串文字。如果 command.aliasesundefined`${command.aliases}` 会将其转换为字符串 "undefined"。由于非空字符串在 JavaScript 中是 truthy value,因此它会将其传递给 .addField() 而不是 none

如果你像这样检查花括号内的值:${command.aliases || "none"} 你在它被转换成字符串之前检查 command.aliases 的值,因为它是 undefined,它是一个 { {3}},它回退到 "none"

所以你可以在花括号内检查它,或者你甚至可以去掉那些模板文字,因为它们不是必需的:

const helpcommand = new Discord.MessageEmbed()
     .addField("Command name", command.name)
     .addField("Description", command.description)
     .addField("Aliases", command.aliases || "none")
     .addField("Usage", command.usage || "none")
     .setTimestamp()
     .setFooter(message.member.user.tag, message.author.avatarURL()); 

message.channel.send(helpcommand);