#Unable to see (/) commands
78 messages · Page 1 of 1 (latest)
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);
- What's your exact discord.js
npm list discord.jsand nodenode -vversion? - Not a discord.js issue? Check out #1081585952654360687.
- 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
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!');
},
};
this is my ping.js file
The slash commands arent showing up via discord?
yeah
Bot is running properly though, no errors
Can you show me your deploy-commands.js file?
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
Alr good, make sure after, to run node deploy-commands.jsthat way your commands get registered with dsicord API.
oh okay
Thank you
mhm
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);
}
})();
This is my deploy-commands.js file
require("dotenv").config()
const { token } = process.env
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
Wait so what part of the code do I replace that with
your require
this ?
change it to the first line here
and instead of .setToken(token), .setToken(process.env.token)
so just add the .config()
No. You can't import environment variables from dotenv. It doesn't work that way
Copy this exactly. Delete what you have
wait wait
^^ packages are (generally) static code, they don't read and automatically export variables based on files (eg based on your .env content)
Do I do anything with this line?
yes, we're both telling you that line is entirely wrong
Do I just COMPLETELY replace it with
require('dotenv').config()
That line is not allowed code. { token } doesn't exist for "dotenv"
yes
how about my clientId and guildId?
This
Just add more to the {}
ive done that
then it should work
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
no, you didn't remove the line
eh?
ah, you did but changed the require to process.env
well that's a mix of my and Samtino's approach
wait
I'm not really sure if Samtino's works, since process.env is an object
sorry
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
What do you mean?
which part?
Oh cuz I'm dumb. I shouldn't have put require in it. Just = process.env
wdym by accessing client and guild ids through process.env
is that not what i did-
oh
so like this
no, you were requiring which imports a module/library
as if process.env was a library
Alr now it works
in reality it's an object that contains your env variables, after you run the dotenv .config()
either destructuring (what you showed in the last image) or dot notation (process.env.<name>) works for accessing a value in an object
ah okay
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
Working now?
Yeah
alr