MessageEmbed:无法读取未定义的属性“客户端”
Posted
技术标签:
【中文标题】MessageEmbed:无法读取未定义的属性“客户端”【英文标题】:MessageEmbed: Cannot read property 'client' of undefined 【发布时间】:2020-09-22 04:04:02 【问题描述】:我正在编写一个简单的 discord.js 机器人。以下代码创建一个嵌入:
const Discord = require('discord.js')
require('dotenv/config')
const bot = new Discord.Client();
const token = process.env.TOKEN;
const owner = process.env.OWNER;
let snipe = '.s';
bot.on('ready', async() =>
console.log(`Logged in as $bot.user.tag!`);
);
bot.on('message', msg =>
if (msg.content === `$snipe help`)
const help = new Discord.MessageEmbed()
.setColor('#7289DA')
.setTitle('snipe help commands')
.setAuthor('join the support discord here', 'https://i.ibb.co/4mPgxV9/imageedit-4-8430062590.png', 'https://www.discord.com/')
.addFields(
name: snipe,
value: 'snipes aka shows the last deleted message in that channel'
,
name: `$snipe help`,
value: 'shows this help message'
,
name: `$snipe [argument]`,
value: 'changes the command to "argument" (must be server owner at time of bot addition)'
, )
.addFooter('go to example.org to add this bot to your server')
msg.channel.send(help)
)
bot.login(token);
当它运行时,我收到以下错误:
C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\structures\MessageEmbed.js:13
Object.defineProperty(this, 'client', value: message.client );
^
TypeError: Cannot read property 'client' of undefined
at new MessageEmbed (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\structures\MessageEmbed.js:13:60)
at Client.<anonymous> (C:\Users\redbrain\Documents\chatbots\snipe\test.js:18:16)
at Client.emit (events.js:310:20)
at MessageCreateHandler.handle (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:310:20)
at Receiver._receiver.onmessage (C:\Users\redbrain\Documents\chatbots\snipe\node_modules\ws\lib\websocket.js:137:47)
引用 "C:\Users\redbrain\Documents\chatbots\snipe\test.js:18:16"
在第 18 行引用了“新”一词。谁能帮我解决这个问题?
【问题讨论】:
【参考方案1】:这是因为您使用 MessageEmbed
就像使用 discord.js@v12 一样,而您仍在使用 discord.js@v11。以下是解决此问题的方法:
1。升级到 discord.js@v12
如果您决定升级到 v12,您还必须更新代码的其他部分,因为存在一些重大更改:点击 here 了解更多信息。
如果您决定升级,那么您的这部分代码应该可以正常工作,前提是您按照另一个答案中的建议编辑添加数组的代码:请参阅MessageEmbed.addFields()
的文档。
下面是它的外观:
const help = new Discord.MessageEmbed()
.addFields([
...,
...,
...
])
2。继续使用 discord.js@v11
如果您选择继续使用 v11,则需要将您正在使用的类更改为 RichEmbed
,因为 MessageEmbed
仅用于接收嵌入,而不用于创建嵌入。在这个版本中你只能使用.addField()
,所以你最终可能会做这样的事情:
const help = new Discord.RichEmbed()
[ // Put your field objects into an array
name: 'name', // string
value: 'content', // string
isInline: true // ?boolean (default is false)
,
...
].forEach((name, value, isInline) =>
help.addField(name, value, isInline)
) // You can use the Array.forEach method to loop through them
【讨论】:
谢谢!不知何故,我没有意识到,*virtual facepalm.* 升级到 discord.js@latest! 别担心啊哈哈以上是关于MessageEmbed:无法读取未定义的属性“客户端”的主要内容,如果未能解决你的问题,请参考以下文章
类型错误; discord.js 中未定义的“MessageEmbed”
无法读取未定义类型错误的属性“推送”:无法读取未定义错误的属性“推送”
带有 Ionic 4 的 SQLite?无法读取未定义类型错误的属性“then”:无法读取未定义的属性“then”