【问题标题】:Discord Bot generates error:"message is not defined"Discord Bot 生成错误:“消息未定义”
【发布时间】:2020-02-20 06:18:40
【问题描述】:

好吧,我正在尝试为我的机器人(又名 Jarvis)创建一个嵌入,当我启动它时,它显示以下错误:无法执行任务 command.js:ReferenceError:未定义消息

我已经完成了所有工作,尝试使用其他人在 Discord 附属网站上创建的基础来更改命令,但最终没有找到解决方案。

const embed = new Discord.RichEmbed()
  .setTitle("This is your title, it can hold 256 characters")
  .setAuthor("Author Name", "https://i.imgur.com/lm8s41J.png")
  .setColor(0x00AE86)
  .setDescription("This is the main body of text, it can hold 2048 characters.")
  .setFooter("This is the footer text, it can hold 2048 characters", "http://i.imgur.com/w1vhFSR.png")
  .setImage("http://i.imgur.com/yVpymuV.png")
  .setThumbnail("http://i.imgur.com/p2qNFag.png")
  .setTimestamp()
  .setURL("https://discord.js.org/#/docs/main/indev/class/RichEmbed")
  .addField("This is a field title, it can hold 256 characters",
    "This is a field value, it can hold 1024 characters.")
  .addField("Inline Field", "They can also be inline.", true)
  .addBlankField(true)
  .addField("Inline Field 3", "You can have a maximum of 25 fields.", true);

  message.channel.send({embed});```

【问题讨论】:

  • 此代码块是否在消息侦听器中执行?我的猜测是它不是,或者您将消息变量命名为与正常使用不同
  • 您能否尝试改进我的代码以适应引用的这种模式?
  • 如果没有看到您的其余代码,我就忍不住了。您添加的代码的唯一问题(据我所知)是message.channel.send(),它只能在消息侦听器上下文中完成,或者从那里传递给函数。您需要确保您的代码知道消息是什么。

标签: javascript discord.js


【解决方案1】:

您似乎缺少onMessage 侦听器。

试试这个代码:

const Discord = require(`discord.js`);
const client = new Discord.Client();
client.login(<token>);
client.on(`message`, function (message) {
    const embed = new Discord.RichEmbed()
      .setTitle("This is your title, it can hold 256 characters");
      //Reducted the rest of the (.setColor, .setFooter, etc) to save space
    message.channel.send({embed});
});

假设您知道从哪里获得&lt;token&gt;,这里的代码应该正是您所需要的。

【讨论】:

    猜你喜欢
    • 2021-01-28
    • 2021-08-12
    • 2022-01-05
    • 2020-08-25
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 2023-01-12
    • 2015-09-01
    相关资源
    最近更新 更多