Search code examples
javascriptnode.jsdiscorddiscord.js

Discord.js V14 TypeError: Cannot read properties of undefined (reading 'reply')


I'm developing Discrod bot with discord.js, but there is error in execute command..

Err Message = TypeError: Cannot read properties of undefined (reading 'reply')

ping.js

const { SlashCommandBuilder, CommandInteraction, Client, GatewayIntentBits, EmbedBuilder, ButtonStyle, ApplicationCommandType, ApplicationCommandOptionType, Message, Collection } = require('discord.js');

module.exports = {
    data:new SlashCommandBuilder()
        .setName("ping")
        .setDescription("pong"),
    /**
     * @param {Client} client
     * @param {CommandInteraction} interaction
     */
    async execute(client, interaction){
        interaction.reply.({content:["퐁!"]})
    }
}
index.js

const { Client, GatewayIntentBits, EmbedBuilder, ButtonStyle, ApplicationCommandType, ApplicationCommandOptionType, Message, Collection } = require('discord.js');
const client = new Client({intents:[GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]});
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const fs = require('fs');
const { token, clientId, testGuildId, mainGuildId } = require('./config.json')

client.commands = new 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.data.name,command);

}
client.once('ready', async ()=>{
    console.log("봇 준비!");
});

client.on('interactionCreate',async (interaction)=>{
    if(!interaction.isChatInputCommand) return;
    const command = client.commands.get(interaction.commandName);
    if(!command) return;
    try{
        await command.execute()
    }catch(err){
        const commandnotresponseerrembed = new EmbedBuilder()
            .setTitle("봇 오류")
            .setDescription("명령어를 실행하는 데 문제가 있습니다. 밑의 오류 내용을 개발자에게 알려주세요!")
            .setColor("#ff4040")
        const errcontentembed = new EmbedBuilder()
            .setTitle("오류 내용")
            .setDescription(`${err}`)
            .setColor("#ff4040")
        interaction.reply({embeds:[commandnotresponseerrembed, errcontentembed]})
    }
})

const commandJsonData = [
    ...Array.from(client.commands.values()).map(c => c.data.toJSON())
]

const rest = new REST({version: 10}).setToken(token);

(async ()=>{
    try{
        console.log("빗금 명령어 등록 중...")
        await rest.put(
            Routes.applicationGuildCommands(clientId,testGuildId),
            {body:commandJsonData}
        )
        console.log("명령어 등록 완료!")
    }catch(err){
        console.log(err)
        console.log("등록에 실패했습니다. 위 오류 사항을 확인해주세요.")
    }
})();

client.login(token)

oh there is Korean because I'm Korean

I did channel.send() but the output was

ypeError: Cannot read properties of undefined (reading 'channel')


Solution

  • In your command file you use the parameters "client" & "interaction" where as in your index file you call the execute() command without any paramets just change your code to this:

    try{
        await command.execute(client, interaction)
    }catch(err){