簡體   English   中英

類型錯誤:無法讀取未定義的屬性(讀取“回復”)- discord.js v14

[英]TypeError: Cannot read properties of undefined (reading 'reply') - discord.js v14

我的朋友讓我給他做一個 discord 機器人,最近當我在https://discordjs.guide的幫助下開始制作機器人時,在制作 ping.js 時發生錯誤。 當我嘗試調用client.ws.ping時,出現錯誤: TypeError: Cannot read properties of undefined (reading 'ws')

在到處尋找可能的修復方法之后,我偶然發現了這篇文章TypeError: Cannot read properties of undefined (reading 'ws') - Discord.js V14描述了這個問題。

一旦我解決了這個問題,又發生了另一個錯誤,我不知道該怎么做: TypeError: Cannot read properties of undefined (reading 'reply')

平.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`);
    },  
};

完整錯誤:

[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:(主/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);

任何幫助將不勝感激,在此先感謝。

要解決此問題,您需要確保已定義interaction object 並在嘗試使用它之前具有reply屬性。

這是一些代碼示例:

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

嘗試將 (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);

錯誤的部分在這里:

    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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM