![](/img/trans.png)
[英]Discord.js - TypeError: Cannot read properties of undefined (reading 'set')
[英]discord.js - Sending an Embed - TypeError: Cannot read properties of undefined (reading 'execute')
我正在把我的頭骨砸在桌子上試圖弄清楚這一點。 如果有人有任何幫助或建議,我會很高興。 我目前只是試圖推出一個非常簡單的測試嵌入,但運氣為 0。 我對 JS 很陌生,對 discord API 也很陌生。 任何願意幫助我的專業人士將不勝感激!
編輯:輸入“~stoplight”是我試圖填充嵌入的操作。
這是主要的“bot.js”腳本
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const prefix = '~';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('QQ is Online!');
});
//these require a ~ prefix
client.on('messageCreate', messageCreate => {
if(!messageCreate.content.startsWith(prefix) || messageCreate.author.bot) return;
const args = messageCreate.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(messageCreate, args);
}
else if(command === 'stoplight'){
client.commands.get('stoplight').execute(messageCreate, args, Discord);
}
});
這是我試圖推出的超級簡單的測試嵌入。 它是一個名為“stoplight.js”的單獨文件
module.exports = {
name: 'test name',
description: 'test desc',
execute(messageCreate, args, Discord) {
const Embed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Test Title')
.setDescription('Test desc.');
channel.send(Embed);
}
}
最后,這是我不斷收到的錯誤......
C:\Users\CueQ\Desktop\Discord Bot\bot.js:34
client.commands.get('stoplight').execute(messageCreate, args, Discord);
^
TypeError: Cannot read properties of undefined (reading 'execute')
at Client.<anonymous> (C:\Users\CueQ\Desktop\Discord Bot\bot.js:34:35)
at Client.emit (node:events:539:35)
at MessageCreateAction.handle (C:\Users\CueQ\Desktop\Discord Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:26:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\CueQ\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\CueQ\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
at WebSocketShard.onPacket (C:\Users\CueQ\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\CueQ\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\CueQ\Desktop\Discord Bot\node_modules\discord.js\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:527:28)
at Receiver.receiverOnMessage (C:\Users\CueQ\Desktop\Discord Bot\node_modules\discord.js\node_modules\ws\lib\websocket.js:1160:20)
在服務器上鍵入“~stoplight”后出現錯誤。 "~ping" 很有效。 所以我很茫然......提前謝謝~
好的,這是一系列問題,但我到了那里。 感謝 Ewong 讓我走上了正確的道路。
問題 1: command.name
是一個測試名稱。 把它換成了stoplight
問題 2:拼寫錯誤的description
。 經典的
問題 3: channel.send(Embed);
在spotlight.js
中是舊語法。 將其更改為messageCreate.channel.send({ embeds: [Embed] });
bot.js
代碼保持不變,新的stoplight.js
代碼如下所示。
module.exports = {
name: 'stoplight',
description: 'test desc',
execute(messageCreate, args, Discord) {
const Embed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Test Title')
.setDescription('Test desc.');
messageCreate.channel.send({ embeds: [Embed] });
}
}
謝謝大家的幫助~
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.