首页 > 解决方案 > Discord Bot 生成错误:“消息未定义”

问题描述

好吧,我试图为我的机器人(又名 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});```

标签: javascriptdiscord.js

解决方案


看起来你错过了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});
});

假设您知道从哪里获取<token>此代码应该正是您所需要的。


推荐阅读