Getting started

Installation

Use the package manager npm to install @mxgnus/slashcommands.js.

npm i @mxgnus/slashcommands.js

Create a new slashcommand

const {
   GuildSlashCommand,
   Slashcommand,
   Slash,
} = require('@mxgnus/slashcommands.js');
new Slash(bot /* your discord.js client */); // initialize the slash command

// create a guild slashcommand
new GuildSlashCommand()
   .setGuildId('your guild id')
   .setName('commandname')
   .setDescription('command description')
   .register();

// create a global slashcommand
new Slashcommand()
   .setName('commandname')
   .setDescription('command description')
   .register();

Create a new slashcommand with options

// create a guild slashcommand with options
const {
   GuildSlashCommand,
   Slash,
   SlashcommandOption,
} = require('@mxgnus/slashcommands.js');

new Slash(bot /* your discord.js client */); // initialize the slash command

const nameOption = new SlashcommandOption()
   .setName('name')
   .setDescription('Name')
   .setType('STRING')
   .setRequired(true);

new GuildSlashCommand()
   .setGuildId('your guild id')
   .setName('name')
   .setDescription('Enter your name')
   .addOption(nameOption)
   .register();

// create a global slashcommand with options
const {
   SlashCommand,
   Slash,
   SlashcommandOption,
} = require('@mxgnus/slashcommands.js');

const nameOption = new SlashcommandOption()
   .setName('name')
   .setDescription('Name')
   .setType('STRING')
   .setRequired(true);

new SlashCommand()
   .setName('name')
   .setDescription('Enter your name')
   .addOption(nameOption)
   .register();

Create a new slashcommand with options and choices

// create a guild slashcommand with options and choices
const {
   GuildSlashCommand,
   Slash,
   SlashcommandOption,
   SlashCommandOptionChoice,
} = require('@mxgnus/slashcommands.js');

new Slash(bot /* your discord.js client */); // initialize the slash command

const nameChoice1 = new SlashCommandOptionChoice()
   .setName('name1')
   .setValue('Name 1');
const nameChoice2 = new SlashCommandOptionChoice()
   .setName('name2')
   .setValue('Name 2');
const nameOption = new SlashcommandOption()
   .setName('name')
   .setDescription('Name')
   .setType('STRING')
   .setRequired(true)
   .setChoices([nameChoice1, nameChoice2]);

new GuildSlashCommand()
   .setGuildId('your guild id')
   .setName('name')
   .setDescription('Enter your name')
   .addOption(nameOption)
   .register();

// create a global slashcommand with options and choices
const {
   SlashCommand,
   Slash,
   SlashcommandOption,
   SlashCommandOptionChoice,
} = require('@mxgnus/slashcommands.js');

const nameChoice1 = new SlashCommandOptionChoice()
   .setName('name1')
   .setValue('Name 1');
const nameChoice2 = new SlashCommandOptionChoice()
   .setName('name2')
   .setValue('Name 2');
const nameOption = new SlashcommandOption()
   .setName('name')
   .setDescription('Name')
   .setType('STRING')
   .setRequired(true)
   .setChoices([nameChoice1, nameChoice2]);

new SlashCommand()
   .setName('name')
   .setDescription('Enter your name')
   .addOption(nameOption)
   .register();

Fetch slashcommands

const {
   fetchGuildSlashcommands,
   fetchSlashcommands,
   Slash,
} = require('@mxgnus/slashcommands.js');
new Slash(bot /* your discord.js client */); // initialize the slash command

// fetch all guild slashcommands
const guildSlashCommands = await fetchGuildSlashcommands();

// fetch all global slashcommands
const slashCommands = await fetchSlashcommands();

Delete slashcommands

const {
   deleteGuildSlashcommand,
   deleteSlashcommand,
   Slash,
} = require('@mxgnus/slashcommands.js');
new Slash(bot /* your discord.js client */); // initialize the slash command

// delete a guild slashcommand by name
deleteGuildSlashcommand({
   guildId: 'your guild id',
   name: 'command name',
});

// delete a guild slashcommand by id
deleteGuildSlashcommand({
   guildId: 'your guild id',
   id: 'command id',
});

// delete a global slashcommand by name
deleteSlashcommand({
   name: 'command name',
});

// delete a global slashcommand by id
deleteSlashcommand({
   id: 'command id',
});

Delete all slashcommands

const {
   deleteAllGuildSlashcommands,
   deleteAllSlashcommands,
   Slash,
} = require('@mxgnus/slashcommands.js');
new Slash(bot /* your discord.js client */); // initialize the slash command

// Delete all guild slashcommands
deleteAllGuildSlashcommands({
   guildId: 'your guild id'
});

// Delete all global slashcommands
deleteAllSlashcommands()

Respond to a slashcommand

bot.on('interactionCreate', async (interaction) => {
   if (interaction.isCommand()) {
      if (interaction.commandName === 'test') {
         interaction.reply('test');
      }
   }
});

Need help?

WARNING

Discord takes a lot of time to create or update a slashcommand. So be patient if you add one.

Guild slashcommands should update directly

Also you need to invite your bot with the application.commands permission:

https://discord.com/api/oauth2/authorize?client_id=YOUR_CLIENT_ID&permissions=0scope=applications.commands%20bot

License

ISC

Last updated