I am making a Discord bot using slash command, and i am stuck at this point. Here is the code of my index.js file
const commands = []
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
}
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
client.on('ready' , () => {
console.log('I am online')
client.user.setActivity('MUSIC 🎧', {type:'LISTENING'})
})
client.on('interactionCreate', async interaction => {
if(!interaction.isCommand())return
const command = interaction.commands.get(interaction.commandName);
if(!command)return
try {
await command.execute(interaction)
} catch (err) {
console.error(err)
await interaction.reply('There was an error trying to execute that command!')
}
})
And a ping.js file to send simple PING-PONG message.
const { SlashCommandBuilder } = require('@discordjs/builders')
module.exports = {
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("PING-PONG"),
async execute (interaction) {
await interaction.reply("Pong!")
}
}
ERROR is:
const command = interaction.commands.get(interaction.commandName);
^
TypeError: Cannot read properties of undefined (reading 'get')
at Client.<anonymous> (C:\vs\Adele music bot\index.js:52:42)
at Client.emit (node:events:520:28)
Everything is fine, it registers slash commands but as soon as i use /ping it just shows the above error.
It seems like you have copied and pasted code from different places and just hoped it worked. The error means interaction.commands
is undefined, which it is.
From what I can see, you wanted a Discord.Collection
with all your interaction commands within. For the purposes of this, we will use client.commands
.
Code sample
/* Create the collection */
client.commands = new Discord.Collection()
const commands = []
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
/* Add data to the collection */
client.commands.set(command.name, command);
}
...
/* Get the command */
const command = client.commands.get(interaction.commandName);
const {Collection} = require('discord.js');
import the collection from discord package.
then after creating the client
client.commands = new Collection();
use the above snippet
then
const command = client.commands.get(interaction.commandName);
write this line where the error originates.
basically you first need to make a new collection of commands for the client. then you need to add the data to add data to the collection but using below command.
client.commands.set();
then after this only you can use the get command
client.commands.set();
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.