#Basehamer999 - Commands

1 messages · Page 1 of 1 (latest)

paper knoll
#

@radiant finch

radiant finch
#

sup

paper knoll
#

interactionCreate is an event

radiant finch
#

sorry if i sound really stupid

paper knoll
#

You should have 1 handler

radiant finch
#

so have it check one time for all commands?

paper knoll
#

So:

interactionCreate {
  if command {
    do command stuff
  } else if autocomplete {
    do autocomplete stuff
  }
}
#

Autocompletes in slash commands, are also 'interactions'

#

So you have to handle the 1 event, for the thing it is

radiant finch
#

like that?

#

i dont really know what i am doing there its kinda confusing to me

paper knoll
#

Yea...so instead of having:

if faq
else if two
else if three

What most (and I) do is import all the commands, put them in client.commands and then we find that based on name, and run that directly.

client.on('interactionCreate', async interaction => {
    console.log(`Interaction for '${interaction.commandName}'`);
    if (!interaction.isCommand()) {
      console.log('Interaction is not a command');
      return;
    }

    // Get the command by name
    const command = client.commands.get(interaction.commandName);

    if (!command) {
      console.log('Command is unknown');
      return;
    }

    try {
        console.log('Run execute for command');
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});
radiant finch
paper knoll
#

So notice you did client.commands = new Collection(); but did nothing with it

radiant finch
#

3 commands responds are stored inside the commands folder+his respond so i have to call those plus add the other command

#

i really losed with what i am doing now

paper knoll
#

You're half there, but you're trying to implement something new, and keep the old; you can't do that easily.

radiant finch
#

So I have to move every respond to the main.js?

paper knoll
#

Your main.js should just handle running the command's execute function

#

As I have here:

client.on('interactionCreate', async interaction => {
  // Get the command by name
  const command = client.commands.get(interaction.commandName);

  // Run the command's function code
  await command.execute(interaction);
});
radiant finch
#

So having the actually respond somewhere else?

paper knoll
#

In the command file itself, remember we added execute to it

radiant finch
#

So have faq.js the respond and the command?

#

Or have faq-respond.js?

paper knoll
#

Not sure on this obsession with respond

#

Whatever it does, it goes in the command file

radiant finch
#

So how would my command faq file look then?

paper knoll
#
import ...;

modules.exports = {
  data: new SlashCommandBuilder(...),
  execute: async function (interaction) {
    // do stuff
  },
}
#

The 'Command' files just handle their data and their execution

paper knoll
#

Ping me if you need more info

radiant finch
#

@paper knoll i tried to figure it out myself but i dont know how i could have responds of 3 other commands that have a other way of responding

#

i dont know if there is a way to have the respond just like other commands in the file with the command itself

#
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('modpack')
        .setDescription('Send the modpack link'),
    async execute(interaction) {
        await interaction.reply('https://www.curseforge.com/minecraft/modpacks/survival-voxel');
    },
};```like this
paper knoll
#

Sorry what are you asking, what do you mean by "have respondes of 3 other commands"?

radiant finch
paper knoll
#

What do you mean options, like a choice of what to FAQ about?

radiant finch
#

yea like /faq one /faq two

paper knoll
#
  .addStringOption(option =>
    option
      .setName('with'.toLowerCase())
      .setDescription('What you want Help with.')
      .setRequired(true)
      .addChoices(...)
  )
radiant finch
paper knoll
#
const withChosen = interaction.options.getString('with'.toLowerCase());

Getter ⬆️

radiant finch
#

i know i have client.on 2 times but i dont know how i would combine it

paper knoll
#
client.commands = new Collection();
 
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

You don't do anything with commandFiles you want to stick them in client.commands so that on interactionCreate you can go:

let command = client.commands.get(interaction.commandName);
...
await command.execute(
  interaction,
);
radiant finch
#

i am getting confused

#

why is javascript so much harder then lua

#

maybe i need to start over

paper knoll
#

You don't need to start over.

#

Go back to the guide, it explains slash commands

radiant finch
#

yea i understand that its just the part that explains the option dont match with the first part

paper knoll
#

What's wrong with the options.
If you have an option e.g. like line called with then you just access it with what I shared earlier when your command's function runs

radiant finch
#

but they explain how to get the respond in the main file but the other part explains how to get it in a seperate file

paper knoll
#

Are you referring to this bit?

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const { commandName } = interaction;

    if (commandName === 'ping') {
        await interaction.reply('Pong!');
    } else if (commandName === 'beep') {
        await interaction.reply('Boop!');
    }
});
radiant finch
#

that was first after that he explained to move it to a specefic file in the commands folder

paper knoll
#

Yea, it's explaining in steps.

#

First just saying "look you can use commandName to run the right code"

#

Then it advances to using a collection, to run the command via a file, rather than if/else if/else if/else if/else if/else if/else if/else if/else if

radiant finch
#

yep and i understand that

#

but the option command dont explain how to add them inside his own file

paper knoll
#

What do you mean "option command"

#

Share a screenshot or code of the bit in the guide you're referring to

radiant finch