#boilerplate

1 messages · Page 1 of 1 (latest)

twin nacelle
#

i'll send you my boilerplate code

#

index.js

const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { Client, Intents, Collection } = require('discord.js');

const dotenv = require('dotenv');

/**
 * Get .env variables
 */

dotenv.config();
const {
  TOKEN, DEV_ID,
} = process.env;

/**
 * Discord.js setup
 */
// Create a new client instance
const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});

// Creating a collection for commands in client
client.commands = new Collection();

/**
 * Import commands found in /commands
 */
const commandFiles = fs.readdirSync('./commands').filter((file) => file.endsWith('.js'));
const commands = [];

/* eslint-disable */
for (const file of commandFiles) {
  const command = require(`./commands/${file}`);
  commands.push(command.data.toJSON());
  client.commands.set(command.data.name, command);
}
/* eslint-enable */

/**
 * Register the commands once bot is ready.
 */
client.once('ready', () => {
  console.log('Ready!');
  const CLIENT_ID = client.user.id;
  const rest = new REST({
    version: '9',
  }).setToken(TOKEN);
  (async () => {
    try {
      if (!DEV_ID) {
        await rest.put(Routes.applicationCommands(CLIENT_ID), {
          body: commands,
        });
        console.log('Successfully registered application commands globally');
      } else {
        const list = await rest.put(Routes.applicationGuildCommands(CLIENT_ID, DEV_ID), {
          body: commands,
        });
        // logges to console commands that are registered
        list.forEach((c) => console.log(c.name, c.options || 'no options'));
        console.log('Successfully registered application commands for development guild');
      }
    } catch (error) {
      if (error) console.error(error);
    }
  })();
});

/**
 * Listen for the commands
 */
client.on('interactionCreate', async (interaction) => {
  // Ignore bots
  if (interaction.user.bot) return;
  // Ignore message if not command.
  if (!interaction.isCommand()) return;

  // Check they are using correct channel, responds with ephemeral notification to correct channel.

  const command = client.commands.get(interaction.commandName);
  if (!command) return;

  try {
    await command.execute(interaction, client);
  } catch (error) {
    if (error) console.error(error);
    await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
  }
});

/**
 * Log bot in.
 */
client.login(TOKEN);
#

.env

TOKEN=xxx
CHANNEL=xxx
DEV_ID=xxx
#

./commands/trade.js

const dotenv = require('dotenv');

dotenv.config();
const { CHANNEL } = process.env;

module.exports = {
  data: new SlashCommandBuilder()
    .setName('trade')
    .setDescription('description'),
  async execute(interaction) {

    await interaction.reply({ content: 'reply to command', ephemeral: true });
  },
};
#

@rocky kayak

rocky kayak
#

wheres the thing that deploys the command

twin nacelle
#

the code is pretty well commented man, you'll find your way, copy those into .js files and you'll figure it out

rocky kayak
#

i dont think you understand what i need help with

#

ok wait