简体   繁体   English

类型错误:无法读取未定义的属性(读取“回复”)- discord.js v14

[英]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.

相关问题 错误 discord.js v14(类型错误:无法读取未定义的属性(读取“标志”)) - Erro discord.js v14 (TypeError: Cannot read properties of undefined (reading 'FLAGS')) 未处理的拒绝:TypeError:无法读取未定义的属性(读取“0”)在尝试打印 discord.js v14 中的 JSON object 时出错 - Unhandled Rejection: TypeError: Cannot read properties of undefined (reading '0') getting error while trying to print JSON object in discord.js v14 无法读取未定义的属性(读取“用户”)| 交互.client.api.users | Discord.js V14 - Cannot read properties of undefined (reading 'users') | interaction.client.api.users | Discord.js V14 Discord.js - TypeError:无法读取未定义的属性(读取“设置”) - Discord.js - TypeError: Cannot read properties of undefined (reading 'set') TypeError:无法读取未定义的属性(读取“设置”)Discord.js - TypeError: Cannot read properties of undefined (reading 'set') Discord.js Discord.js:TypeError:无法读取未定义的属性(读取“添加”) - Discord.js: TypeError: Cannot read properties of undefined (reading 'add') TypeError:无法在 discord.js v13 中读取未定义的属性(读取“路径”) - TypeError: Cannot read properties of undefined (reading 'path') in discord.js v13 TypeError (discord.js v14) - SlashCommandbuilder 不是构造函数 - TypeError (discord.js v14) - SlashCommandbuilder is not a constructor discord.js v13 permissions.has() function 不工作(TypeError: Cannot read properties of undefined (reading 'has')) - discord.js v13 permissions.has() function not working (TypeError: Cannot read properties of undefined (reading 'has')) discord.js - 发送嵌入 - 类型错误:无法读取未定义的属性(读取“执行”) - discord.js - Sending an Embed - TypeError: Cannot read properties of undefined (reading 'execute')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM