[英]TypeError: Cannot read properties of undefined (reading 'reply') - discord.js v14
my friend asked me to make him a discord bot and recently when I started making the bot with the help of https://discordjs.guide , an error occured while making ping.js.我的朋友让我给他做一个 discord 机器人,最近当我在https://discordjs.guide的帮助下开始制作机器人时,在制作 ping.js 时发生错误。 When i try calling client.ws.ping
, I get an error: TypeError: Cannot read properties of undefined (reading 'ws')
当我尝试调用client.ws.ping
时,出现错误: TypeError: Cannot read properties of undefined (reading 'ws')
After looking everywhere for a possible fix, I've stumbled upon this post TypeError: Cannot read properties of undefined (reading 'ws') - Discord.js V14 describing that issue.在到处寻找可能的修复方法之后,我偶然发现了这篇文章TypeError: Cannot read properties of undefined (reading 'ws') - Discord.js V14描述了这个问题。
Once I got that fixed, another error occured and I'm not sure what to do here: TypeError: Cannot read properties of undefined (reading 'reply')
一旦我解决了这个问题,又发生了另一个错误,我不知道该怎么做: TypeError: Cannot read properties of undefined (reading 'reply')
ping.js:平.js:
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Display the latency'),
async execute(client, interaction) {
await interaction.reply(`Latency is ${Date.now() - interaction.createdTimestamp}ms. API Latency is ${client.ws.ping}ms`);
},
};
Full error:完整错误:
[androser@arch Manmade]$ node .
Ready! Logged in as MortosBot#0850
TypeError: Cannot read properties of undefined (reading 'reply')
at Object.execute (/home/androser/Desktop/MortisBot/Manmade/commands/ping.js:9:21)
at Client.<anonymous> (/home/androser/Desktop/MortisBot/Manmade/mortis.js:30:17)
at Client.emit (node:events:525:35)
at InteractionCreateAction.handle (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
at WebSocketShard.onPacket (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/websocket/WebSocketShard.js:489:22)
at WebSocketShard.onMessage (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/websocket/WebSocketShard.js:328:10)
at callListener (/home/androser/Desktop/MortisBot/Manmade/node_modules/ws/lib/event-target.js:290:14)
at WebSocket.onMessage (/home/androser/Desktop/MortisBot/Manmade/node_modules/ws/lib/event-target.js:209:9)
Error executing ping
TypeError: Cannot read properties of undefined (reading 'reply')
at Object.execute (/home/androser/Desktop/MortisBot/Manmade/commands/ping.js:9:21)
at Object.execute (/home/androser/Desktop/MortisBot/Manmade/events/interactionCreate.js:16:18)
at Client.<anonymous> (/home/androser/Desktop/MortisBot/Manmade/mortis.js:46:44)
at Client.emit (node:events:525:35)
at InteractionCreateAction.handle (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
at WebSocketShard.onPacket (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/websocket/WebSocketShard.js:489:22)
at WebSocketShard.onMessage (/home/androser/Desktop/MortisBot/Manmade/node_modules/discord.js/src/client/websocket/WebSocketShard.js:328:10)
at callListener (/home/androser/Desktop/MortisBot/Manmade/node_modules/ws/lib/event-target.js:290:14)
mortis.js: (main/index.js) mortis.js:(主/index.js)
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
require('dotenv').config()
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
client.login(process.env.TOKEN);
Any help would be appreciated, thanks in advance.任何帮助将不胜感激,在此先感谢。
To fix that you need to make sure interaction
object is defined and has a reply
property before trying to use it.要解决此问题,您需要确保已定义interaction
object 并在尝试使用它之前具有reply
属性。
here is some code example:这是一些代码示例:
async execute(client, interaction) {
if (!interaction || !interaction.reply) return;
await interaction
.reply(`Latency is ${Date.now() - interaction.createdTimestamp}ms. API Latency is ${client.ws.ping}ms`);
}
Try to change (main/index.js) to this:尝试将 (main/index.js) 更改为:
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
require('dotenv').config()
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(client, interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
client.login(process.env.TOKEN);
The wrong part at here:错误的部分在这里:
try {
await command.execute(interaction); // U must add client for it (command.execute(client,interaction))
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.