#Command format

20 messages · Page 1 of 1 (latest)

vagrant cove
#

Hey I'm following the discordjs guide, but when getting to the commands part it shows two ways of writing the commands either like :

const { SlashCommandBuilder } = require('discord.js');

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

or like:

client.on(Events.InteractionCreate, async interaction => {
    if (!interaction.isChatInputCommand()) return;

    if (interaction.commandName === 'ping') {
        await interaction.deferReply();
        await wait(4000)
        await interaction.reply({ 
            content: 'Secret Pong!', 
            ephemeral: true 
        });
    }
});

On the second example the guide tells us to create and event forlder and make some changes to the index file but after that the bot doesnt run as it shows and error, but if I dont make the changes and leave the event code in the index file and revert the code to the first example it runs no problem, so my question is which one is up to date manner to write commands.
(soz for the long post)

queen schooner
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

vagrant cove
still path
#

The first one. The second one is just to have a minimal working example for handling an interaction without confusing readers with all the details of a handler loading files etc. at first

vagrant cove
#

I see, after that the guide has a mixture of both examples so it seems unclear on how to proceed

#

there is an example of a gif command

#
const { SlashCommandBuilder } = require('discord.js');

const data = new SlashCommandBuilder()
    .setName('gif')
    .setDescription('Sends a random gif!')
    .addStringOption(option =>
        option.setName('category')
            .setDescription('The gif category')
            .setRequired(true)
            .addChoices(
                { name: 'Funny', value: 'gif_funny' },
                { name: 'Meme', value: 'gif_meme' },
                { name: 'Movie', value: 'gif_movie' },
            ))
#

But this doesnt work, it isnt returning anything

still path
#

That’s only the definition of the command to be deployed. It’s not the handler that replies to it

vagrant cove
#

So thats basically half of it

still path
#

Yes

vagrant cove
#

Oh okay, and regarding the event folder? should I just ignored that and leave the event handling code in the index file?

#

its seems quite unnecessary to have another folder with a few pieces of code

still path
#

Depends on how many event listeners you‘ll need… if you only add slashcommands and no guildMemberAdd, guildCreate, banCreate, whatNot event handlers then you won‘t need the folder I‘d say

vagrant cove
#

im asking because the type of files in the folder that the guide tells us to make is construction/initiation type of the construct

#

Like so

const { Events } = require('discord.js');

module.exports = {
    name: Events.ClientReady,
    once: true,
    execute(client) {
        console.log(`Ready! Logged in as ${client.user.tag}`);
    },
};
#

which it seesm redundant to do this

#

anyway thanks for your help!

still path
#

What’s redundant about that?

vagrant cove
#

doesnt seem necessary to create another folder and files to move only that piece of code