Skip to content

Instantly share code, notes, and snippets.

@Frontesque
Created January 22, 2021 18:24
Show Gist options
  • Save Frontesque/b24d88f5573c7a52642c23f388c9eed0 to your computer and use it in GitHub Desktop.
Save Frontesque/b24d88f5573c7a52642c23f388c9eed0 to your computer and use it in GitHub Desktop.
Discord.js V12 slash commands manager

Notes

This will only work if a guild grants your bot the applications.commands scope

Notice:

The previous developer (coolmlgplayer-js) has asked me to continue to maintain this gist, as she has quit development.

//This example will create a global slash command called 'ping'
const { Client } = require('discord.js');
const bot = new Client();
const SlashCommandManager = require('./SlashCommandManager');
bot.SlashCommandManager = new SlashCommandManager(bot);
bot.once('ready', async () => {
const commandExists = await bot.SlashCommandManager.globalCommandExists('ping');
if (!commandExists) bot.SlashCommandManager.createGlobalCommand({
name: 'ping',
description: 'pong'
}).catch(console.error);
});
bot.ws.on('INTERACTION_CREATE', async interaction => {
switch (interaction.data.name) {
case "ping":
bot.SlashCommandManager.respond(interaction, {
type: 3,
data: {
content: 'Pong!',
flags: 1 << 6
}
});
break;
default:
bot.SlashCommandManager.respond(interaction, {
type: 3,
data: {
content: `No handler found for \`/${interaction.data.name}\``,
flags: 1 << 6
}
});
break;
};
});
bot.login('TOKEN');
module.exports = class SlashCommandManager {
constructor(client) {
this.client = client;
};
async createGlobalCommand(data = {}) {
return this.client.api.applications(this.client.user.id).commands.post({
data: data
});
}
async createGuildCommand(guild, data = {}) {
if (!guild || !guild.id) throw new TypeError('No guild provided!');
if (!this.client.guilds.cache.has(guild.id)) throw new TypeError('Invalid guild provided!');
return this.client.api.applications(this.client.user.id).guilds(guild.id).commands.post({
data: data
});
}
async globalCommandExists(command_id) {
const commands = await this.client.api.applications(this.client.user.id).commands.get();
return commands.some(c => c.id === command_id || c.name === command_id);
}
async guildCommandExists(guild, command_id) {
if (!guild || !guild.id) throw new TypeError('No guild provided!');
if (!this.client.guilds.cache.has(guild.id)) throw new TypeError('Invalid guild provided!');
const commands = await this.client.api.applications(this.client.user.id).guilds(guild.id).commands.get();
return commands.some(c => c.id === command_id || c.name === command_id);
}
async deleteGlobalCommand(command_id) {
const commands = await this.client.api.applications(this.client.user.id).commands.get();
const command = commands.find(c => c.id === command_id || c.name === command_id);
if (command) command_id = command.id
else throw new TypeError('Invalid Command');
return this.client.api.applications(this.client.user.id).commands(command_id).delete();
}
async deleteGuildCommand(guild, command_id) {
if (!guild || !guild.id) throw new TypeError('No guild provided!');
if (!this.client.guilds.cache.has(guild.id)) throw new TypeError('Invalid guild provided!');
const commands = await this.client.api.applications(this.client.user.id).guilds(guild.id).commands.get();
const command = commands.find(c => c.id === command_id || c.name === command_id);
if (command) command_id = command.id
else throw new TypeError('Invalid Command');
return this.client.api.applications(this.client.user.id).guilds(guild.id).commands(command_id).delete();
}
async respond(interaction, data = {}) {
return this.client.api
.interactions(interaction.id, interaction.token)
.callback.post({
data: data
});
}
};
@AshishBytes
Copy link

how to use in cmd handler plz help me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment