#Unable to see (/) commands

78 messages · Page 1 of 1 (latest)

daring canopy
#

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.

#
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);
pulsar mangoBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • 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
daring canopy
#
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

halcyon void
daring canopy
#

Bot is running properly though, no errors

halcyon void
daring canopy
#

oh i see

#

lol sorry, i figured out

halcyon void
halcyon void
#

mhm

daring canopy
#

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

tame jolt
#
require("dotenv").config()
const { token } = process.env
daring canopy
#

huh?

#

so instead of

const { clientId, guildId, token } = require('dotenv');
#

?

tame jolt
distant charm
#

or just process.env.token

#

(after running .config())

daring canopy
#

Wait so what part of the code do I replace that with

distant charm
#

your require

distant charm
#

and instead of .setToken(token), .setToken(process.env.token)

daring canopy
#

so just add the .config()

tame jolt
#

No. You can't import environment variables from dotenv. It doesn't work that way

tame jolt
daring canopy
#

wait wait

distant charm
daring canopy
distant charm
#

yes, we're both telling you that line is entirely wrong

daring canopy
#

Do I just COMPLETELY replace it with

require('dotenv').config()
tame jolt
#

That line is not allowed code. { token } doesn't exist for "dotenv"

distant charm
#

yes

daring canopy
#

how about my clientId and guildId?

tame jolt
#

Just add more to the {}

distant charm
#

then it should work

daring canopy
#
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

distant charm
#

no, you didn't remove the line

daring canopy
#

eh?

distant charm
#

ah, you did but changed the require to process.env

#

well that's a mix of my and Samtino's approach

daring canopy
#

wait

distant charm
#

I'm not really sure if Samtino's works, since process.env is an object

daring canopy
#

sorry

distant charm
#

but if it works then that code should work

#

even though it's a mix

daring canopy
distant charm
#

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

daring canopy
#

What do you mean?

distant charm
#

which part?

tame jolt
daring canopy
#

is that not what i did-

distant charm
#

as if process.env was a library

daring canopy
#

Alr now it works

distant charm
#

in reality it's an object that contains your env variables, after you run the dotenv .config()

daring canopy
#

Aight thank you

#

it works

distant charm
#

either destructuring (what you showed in the last image) or dot notation (process.env.<name>) works for accessing a value in an object

tame jolt
halcyon void
daring canopy
halcyon void
#

alr