javascript - 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});```
解决方案
看起来你错过了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>
此代码应该正是您所需要的。
推荐阅读
- c# - C# WPF XAML 工具包,单击按钮切换视图
- mysql - INDEX 没有改善 sql 查询结果
- arrays - 如何使用 json 对象中的数组以及不在 json 对象中的数组在 React 应用程序中设置状态?
- git - Git flow-需要从另一个分支创建功能分支
- javascript - 在 mouseenter 上使用 javascript 旋转 div 并将它们旋转回 mouseleave 上的原始位置
- javascript - 条目存在时删除 Typescript 界面列表中的元素,始终显示负索引
- spring-mvc - 当 HTML 没有“PUT”时,如何以“PUT”方法发送文件?
- c++ - Eigen::Map 未命名?
- wordpress - WP REST API 无法设置帖子标签
- c - 函数指针地址总是静态的吗?