Unable to see (/) commands

I'm trying to follow the guide on the part of creating slash commands, and this is my main.js file, and my bot is up and running correctly, but the slash command won't show up.
59 Replies
Aeasela
AeaselaOP6mo ago
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const dotenv = require('dotenv');

dotenv.config();

const client = new Client({ intents: [ GatewayIntentBits.Guilds] });

client.commands = new Collection();

const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
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.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async 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);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing the command.', flags: MessageFlags.Ephemeral });
} else {
await interaction.reply({ content: 'There was an error while executing the command.', flags: MessageFlags.Ephemeral });
}
}
});

client.login(process.env.token);
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const dotenv = require('dotenv');

dotenv.config();

const client = new Client({ intents: [ GatewayIntentBits.Guilds] });

client.commands = new Collection();

const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
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.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});

client.on(Events.InteractionCreate, async 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);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing the command.', flags: MessageFlags.Ephemeral });
} else {
await interaction.reply({ content: 'There was an error while executing the command.', flags: MessageFlags.Ephemeral });
}
}
});

client.login(process.env.token);
d.js toolkit
d.js toolkit6mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
Aeasela
AeaselaOP6mo ago
this is my index.js file
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong.'),
async execute(interaction) {
await interaction.reply('Pong!');
},
};
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong.'),
async execute(interaction) {
await interaction.reply('Pong!');
},
};
this is my ping.js file
! marek
! marek6mo ago
The slash commands arent showing up via discord?
Aeasela
AeaselaOP6mo ago
yeah Bot is running properly though, no errors
! marek
! marek6mo ago
Can you show me your deploy-commands.js file?
Aeasela
AeaselaOP6mo ago
I haven't gotten to that step yet, I'm following the guide- oh am i supposed to finish that step first- oh i see lol sorry, i figured out
! marek
! marek6mo ago
Alr good, make sure after, to run node deploy-commands.jsthat way your commands get registered with dsicord API.
Aeasela
AeaselaOP6mo ago
oh okay Thank you
! marek
! marek6mo ago
mhm
Aeasela
AeaselaOP6mo ago
When running node deploy-commands.js, i see the "Started refreshing 2 application (/) commands." message, but then I receive an error stating: Expected token to be set for this request, but none was present I have my token requiring .env..
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require ('dotenv');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
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) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" propery.`);
}
}
}

const rest = new REST().setToken(token);

(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require ('dotenv');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
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) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" propery.`);
}
}
}

const rest = new REST().setToken(token);

(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
This is my deploy-commands.js file
Samtino
Samtino6mo ago
require("dotenv").config()
const { token } = process.env
require("dotenv").config()
const { token } = process.env
Aeasela
AeaselaOP6mo ago
huh? so instead of
const { clientId, guildId, token } = require('dotenv');
const { clientId, guildId, token } = require('dotenv');
?
Samtino
Samtino6mo ago
You can't import environment variables from "dotenv" instead, you can import them from process.env which is the global command to access the loaded environment variables
Amgelo
Amgelo6mo ago
or just process.env.token (after running .config())
Aeasela
AeaselaOP6mo ago
Wait so what part of the code do I replace that with
Amgelo
Amgelo6mo ago
your require
Aeasela
AeaselaOP6mo ago
this ?
Amgelo
Amgelo6mo ago
change it to the first line here and instead of .setToken(token), .setToken(process.env.token)
Aeasela
AeaselaOP6mo ago
so just add the .config()
Samtino
Samtino6mo ago
No. You can't import environment variables from dotenv. It doesn't work that way Copy this exactly. Delete what you have
Aeasela
AeaselaOP6mo ago
wait wait
Amgelo
Amgelo6mo ago
^^ packages are (generally) static code, they don't read and automatically export variables based on files (eg based on your .env content)
Aeasela
AeaselaOP6mo ago
Do I do anything with this line?
Amgelo
Amgelo6mo ago
yes, we're both telling you that line is entirely wrong
Aeasela
AeaselaOP6mo ago
Do I just COMPLETELY replace it with
require('dotenv').config()
require('dotenv').config()
Samtino
Samtino6mo ago
That line is not allowed code. { token } doesn't exist for "dotenv"
Amgelo
Amgelo6mo ago
yes
Aeasela
AeaselaOP6mo ago
how about my clientId and guildId?
Amgelo
Amgelo6mo ago
^^
Samtino
Samtino6mo ago
This Just add more to the {}
Aeasela
AeaselaOP6mo ago
ive done that
Amgelo
Amgelo6mo ago
then it should work
Aeasela
AeaselaOP6mo ago
const { REST, Routes } = require('discord.js');
require('dotenv').config();
const { clientId, guildId } = require(process.env);
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
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) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" propery.`);
}
}
}

const rest = new REST().setToken(process.env.token);

(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
const { REST, Routes } = require('discord.js');
require('dotenv').config();
const { clientId, guildId } = require(process.env);
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
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) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" propery.`);
}
}
}

const rest = new REST().setToken(process.env.token);

(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
so my new code would be
Amgelo
Amgelo6mo ago
no, you didn't remove the line
Aeasela
AeaselaOP6mo ago
eh?
Amgelo
Amgelo6mo ago
ah, you did but changed the require to process.env well that's a mix of my and Samtino's approach
Aeasela
AeaselaOP6mo ago
wait
Amgelo
Amgelo6mo ago
I'm not really sure if Samtino's works, since process.env is an object
Aeasela
AeaselaOP6mo ago
sorry
Amgelo
Amgelo6mo ago
but if it works then that code should work even though it's a mix
Aeasela
AeaselaOP6mo ago
No description
Amgelo
Amgelo6mo ago
try removing const { clientId, guildId } = require(process.env); then and instead access your client and guild ids through the process.env object like in your setToken
Aeasela
AeaselaOP6mo ago
What do you mean?
Amgelo
Amgelo6mo ago
which part?
Samtino
Samtino6mo ago
Oh cuz I'm dumb. I shouldn't have put require in it. Just = process.env
Aeasela
AeaselaOP6mo ago
wdym by accessing client and guild ids through process.env is that not what i did- oh
Aeasela
AeaselaOP6mo ago
so like this
No description
Amgelo
Amgelo6mo ago
no, you were requiring which imports a module/library as if process.env was a library
Aeasela
AeaselaOP6mo ago
Alr now it works
Amgelo
Amgelo6mo ago
in reality it's an object that contains your env variables, after you run the dotenv .config()
Aeasela
AeaselaOP6mo ago
Aight thank you it works
Amgelo
Amgelo6mo ago
either destructuring (what you showed in the last image) or dot notation (process.env.<name>) works for accessing a value in an object
Aeasela
AeaselaOP6mo ago
ah okay
Samtino
Samtino6mo ago
Destructuring is nice if you need multiple variables in the same file. But dot notation doesn't require any imports in any subfiles Both are correct, and have benefits and downsides for each
! marek
! marek6mo ago
Working now?
Aeasela
AeaselaOP6mo ago
Yeah
! marek
! marek6mo ago
alr
user1172269752840888360
usually when this happens to me is just because my discord client is using the cached commands from my bot (before registering them), so the fix is simply restarting it (ctrl + R)

Did you find this page helpful?