Discord.js 机器人的代码响应错误“TypeError:”
Posted
技术标签:
【中文标题】Discord.js 机器人的代码响应错误“TypeError:”【英文标题】:Code for Discord.js bot responds with error "TypeError:" 【发布时间】:2020-06-23 23:36:17 【问题描述】:我正在测试使用 Discord.js 为 Discord Bot 发送嵌入消息,这基本上是一个用于与 Discord API 交互的 node.js 模块。这是我用于机器人发送嵌入消息的代码:
const Discord = require('discord.js');
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);
当我运行代码时,我在 Visual Studio Code IDE 中收到此错误:
类型错误:(中间 value).setTitle(...).setAuthor(...).setColor(...).setDescription(...).setFooter(...).setImage(...).setThumbnail(... ).setTimestamp(...).setURL(...).addField(...).addField(...).addBlankField 不是函数
【问题讨论】:
【参考方案1】:当您查看documentation 时,MessageEmbed
类中没有addBlankField()
函数,请检查您的discord.js
版本。
从 v12.0.0 开始,他们将 RichEmbed
更改为 MessageEmbed
。
【讨论】:
【参考方案2】:试试这个:
const Discord = require('discord.js');
const client = new Discord.Client();
client.login('Your bot\'s token here');
client.on('ready', () => console.log('I\'m ready !');
client.on('message', message =>
const embed = new Discord.MessageEmbed()
.setTitle("This is your title, it can hold 256 characters")
.setAuthor("Author Name", "https://i.imgur.com/lm8s41J.png")
.setColor('WHITE')
.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)
.addField("Inline Field 3", "You can have a maximum of 25 fields.", true);
message.channel.send(embed);
如果这不起作用,我建议点击here查看官方文档
【讨论】:
【参考方案3】:如果您想以其他方式添加空白字段,因为 discord.js v12 不支持 .addBlankField()
。你可以这样写:
.addField("** **", "** **")
这将添加一个标题和描述几乎为空的字段(在您发送嵌入后它们都包含一个空格)
【讨论】:
不确定这是否可行,您可以使用.addField('\u200B', '\u200B');
来解决问题
@NotTrixxie ** **
可以。我一直使用这个技巧。以上是关于Discord.js 机器人的代码响应错误“TypeError:”的主要内容,如果未能解决你的问题,请参考以下文章
Discord.js - Discord 机器人停止响应命令