【问题标题】:Calling a constant from another file从另一个文件调用常量
【发布时间】:2017-11-29 04:53:02
【问题描述】:

我在不同文件中遇到常量问题:我必须将嵌入存储在另一个文件中,然后在主文件中调用它,但是当我尝试这样做时,它会给我解析错误,例如 Unexpected '.' in 'help.embed' I尝试使用这些线程 [1,2] 中建议的方法,但它们没有用:它继续给我解析错误。有人可以帮我吗?
PS:我不想使用 HTML 文件来调用脚本,我只想使用 JS 和 JSON

这是我的代码(简化版):

// help.js
const Discord = require("discord.js");

var embed = new Discord.RichEmbed()
  .setTitle("Title")

module.exports = Object.freeze({
  embed: embed
});
// main.js
const help = require("./help.js")
client.on("ready", () =>{
  client.channels.find("id",config.disaply_channel).send(help.embed); //Parse error
});

【问题讨论】:

  • 您确定help 对象可以正常通过吗?将embed 属性记录到控制台时,您看到了吗?
  • 试试.send({help.embed})
  • 我记录了帮助对象,我清楚地看到了嵌入属性,类型为 RichEmbed。我也尝试只记录 help.embed ,它是一个普通对象。但是当我尝试发送它时,它返回“无法发送空消息”或类似的东西

标签: javascript discord discord.js


【解决方案1】:

我发现如果命令与嵌入在同一个文件中,我应该使用message.channel.send({embed}),所以我尝试发送help.{embed},但它给了我一个致命错误。 我解决了声明导出embed: {embed} 而不仅仅是embed: embed,然后在主文件中调用help.embed。还是谢谢你的支持

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多