Discord Bot 嵌入变量以未定义的形式返回
Posted
技术标签:
【中文标题】Discord Bot 嵌入变量以未定义的形式返回【英文标题】:Discord Bot Embed Variables come back as undefined 【发布时间】:2019-04-10 13:10:03 【问题描述】:这是我的代码,值一直未定义 https://cdn.discordapp.com/attachments/501784044649054231/509529437419732994/unknown.png
这段代码应该是
!rebirth 1 1 1
它应该在嵌入中回复
集体现金 1 生命币 1 代币 1
exports.run = (_client, message, args) =>
let Cash = args[0]; // Remember arrays are 0-based!.
let Tokens = args[1];
let LifeCoins = args[2];
const Discord = require('discord.js')
var embed = new Discord.RichEmbed()
.setTitle("Rebirth")
.setAuthor("Author Name", `$message.author.avatarURL`)
.setTimestamp()
.addField("Collective Cash", `$Cash`)
.addField("Lifecoins", `$LifeCoins`)
.addField("Tokens", `$Tokens`)
.addBlankField(true)
message.channel.send(
embed
)
【问题讨论】:
【参考方案1】:exports.run = (_client, message, args) =>
let rest_of_the_string = message.content.slice('embed'.length); //removes the first part
let array_of_arguments = rest_of_the_string.split(' '); //[Cash LifeCoints Tokens]
const Discord = require('discord.js')
var embed = new Discord.RichEmbed()
.setTitle("Rebirth")
.setAuthor("Author Name", `$message.author.avatarURL`)
.setTimestamp()
.addField("Collective Cash", array_of_arguments[0])
.addField("Lifecoins", array_of_arguments[1])
.addField("Tokens", array_of_arguments[2])
.addBlankField(true)
message.channel.send(
embed
)
试试看
【讨论】:
【参考方案2】:2 个问题:
第一个问题是最后一行:
message.channel.send(embed);
在 JS 中, embed 与 embed:undefined 相同,因为您没有指定任何值。
在 discord.js 中,只需执行 message.channel.send(embed)
即可。
第二个问题:
你在变量中使用了对象解构, 在您的代码中:
let Cash = args[0]; // Remember arrays are 0-based!.
let Tokens = args[1];
let LifeCoins = args[2];
等于
let Cash = args[0].Cash; // Remember arrays are 0-based!.
let Tokens = args[1].Tokens;
let LifeCoins = args[2].LifeCoins;
显然返回未定义(没有“现金”属性)
相反,您应该这样做:
let Cash = args[0]; // Remember arrays are 0-based!.
let Tokens = args[1];
let LifeCoins = args[2];
代码现在应该如下所示:
exports.run = (_client, message, args) =>
let Cash = args[0]; // Remember arrays are 0-based!.
let Tokens = args[1];
let LifeCoins = args[2];
const Discord = require('discord.js')
var embed = new Discord.RichEmbed()
.setTitle("Rebirth")
.setAuthor("Author Name", `$message.author.avatarURL`)
.setTimestamp()
.addField("Collective Cash", `$Cash`)
.addField("Lifecoins", `$LifeCoins`)
.addField("Tokens", `$Tokens`)
.addBlankField(true)
message.channel.send(embed)
【讨论】:
以上是关于Discord Bot 嵌入变量以未定义的形式返回的主要内容,如果未能解决你的问题,请参考以下文章