简体   繁体   中英

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. When i try calling client.ws.ping , I get an error: 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.

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')

ping.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)

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.

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:

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 });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM