#Tehirion : Help with Get Error.
1 messages · Page 1 of 1 (latest)
this is the code that is erroring
const { Events } = require('discord.js');
module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(`Error executing ${interaction.commandName}`);
console.error(error);
}
},
};
Tehirion : Help with Get Error.
@late anvil I thought I'd do this so that the discussion isn't conveluted
Ok. Show where you declare client.commands.
this is how the files are listed
This is my deploying of command code
const { REST, Routes } = require("discord.js");
require("dotenv").config();
const fs = require("node:fs");
const path = require("node:path");
const token = process.env.TOKEN;
const clientId = process.env.CLIENT_ID;
const guildId = process.env.GUILD_ID;
const commands = [];
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
}
// Construct and prepare an instance of the REST module
const rest = new REST({ version: "10" }).setToken(token);
// and deploy your commands!
(async () => {
try {
console.log(
`Started refreshing ${commands.length} application (/) commands.`
);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands }
);
console.log(
`Successfully reloaded ${data.length} application (/) commands.`
);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
Ok. Show where you define client.commands.
Since I cannot seem to find it, I'm going to assume that is what I have missed lmao
It should be in index.js if you’re following the guide.
Your error says it’s undefined so you need to define it and then load the commands into it so you can execute them.
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] });
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);
This is my complete Index code
Ok. So you’re loading events but your client doesn’t know where or how to see your command code.
right okay
That page will walk you thru that command loading
so essentially I have it setup, it's just I need to give it the access to see the codes essentially?
Thanks @late anvil I do appreciate you helping me out in here!
it's the client Commands = new collections that i've missed isnt it?
That’s part of it
Right I'm starting to see it now
You’ve basically told discord 'hey I have these commands' and discord is like 👍🏻.
You run a command and discord sends it to you as an interaction. But your client doesn’t know how to run them.
Right I get it now.
I'm basically making an announcement for cookies and when the person comes to get the cookie, I haven't even put them on the table basically
The cookies have been made but you don’t know where they are.
RIGHT I GET IT!
WHERE ARE THE COOKIES
THE COOKIES MASON, WHERE ARE THEY?!
Well I hope I can find the cookies 😂
Should be just that little section loading the command files into the commands collection.
@late anvil So this seems to have appeared
The Commands have doubled up and I can't seem to figure why
One bit of good news is that the commands are working again
One set are guild commands and the other set is deployed globally
It looks like you’re deploying guild commands here. You can send an empty array as the body to delete all commands.
I assume I would do this by making the bot not grab the array of commands in the commands folder?
You could just body: []
DAD I DID IT ARE YOU PROUD 😄
Creating Your Bot: Loading command files