More builders to help manage application commands for applications and guilds
Contents:
- Installation
- Example usage
- Classes
3.1. GuildContextMenuCommandBuilder
3.2. GuildSlashCommandBuilder - Functions
4.1. filterGlobalCommands
4.2. filterGuildCommands
npm i .\<local directory>\
Discord.js and its dependencies are required
Credit to discord.js for the original guide to registering slash commands
Use new builders to easily find and deploy application commands to both guilds and your bot globally:
const { GuildSlashCommandBuilder, GuildContextMenuCommandBuilder, filterGlobalCommands, filterGuildCommands } = require("guildbuilders")
const { REST, Routes, SlashCommandBuilder, ApplicationCommandType } = require("discord.js")
const { clientId, guildId, token } = require("./config.json")
// Build a list of commands
const commands = [
// Use .addGuildId and .setGuildIds in addition to regular discord.js builder methods to create application commands
new GuildSlashCommandBuilder()
.setName("hello-guild")
.setDescription("This is a command that will be registered to a guild")
.addGuildId(guildId),
new GuildContextMenuCommandBuilder()
.setName("Guild Message Command")
.setType(ApplicationCommandType.Message)
.setGuildIds([ guildId ]),
// You can still use regular discord.js builders!
new SlashCommandBuilder()
.setName("hello-application")
.setDescription("This is a command that will be globally registered to the bot"),
]
// Construct and prepare an instance of the REST module
const rest = new REST({ version: "10" }).setToken(token)
// and deploy your commands!
(async () => {
try {
// Find commands marked as global
const globalCommands = filterGlobalCommands(commands)
// and deploy them to the bot using the .put method
await rest.put(
Routes.applicationCommands(clientId),
{ body: globalCommands }
)
.then(data => console.log(`Successfully reloaded ${data.length} application commands.`))
// Using .put with a different Route can register commands to a guild
const guildCommands = filterGuildCommands(commands, guildId)
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: guildCommands }
)
.then(data => console.log(`Successfully reloaded ${data.length} application commands to guild with ID ${guildId}.`))
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error)
}
})()
Builders are also compatible with ApplicationCommandManager.set() from Discord.js:
await client.application.commands.set(globalCommands)
.then(data => console.log(`Successfully reloaded ${data.length} application commands.`))
await client.application.commands.set(guildCommands, guildId)
.then(data => console.log(`Successfully reloaded ${data.length} application commands to guild with ID ${guildId}.`))
GuildContextMenuCommandBuilder extends ContextMenuCommandBuilder
new GuildContextMenuCommandBuilder()
All of the guild ids that the command should be registered to
Type: Array <Snowflake>
Whether the application command should be registered to guilds or the application globally
Type: boolean (always true)
Adds a single guild id to the list of guilds the command should be registered to
PARAMETER | TYPE | DESCRIPTION |
---|---|---|
guildId | Snowflake | The id of the guild to add1 |
Returns: GuildContextMenuCommandBuilder
Replace the list of guild ids with a new list
PARAMETER | TYPE | DESCRIPTION |
---|---|---|
guildIds | Array <Snowflake> | The guild ids that the command should be registered to1 |
Returns: GuildContextMenuCommandBuilder
GuildSlashCommandBuilder extends SlashCommandBuilder
new GuildSlashCommandBuilder()
All of the guild ids that the command should be registered to
Type: Array <Snowflake>
Whether the application command should be registered to guilds or the application globally
Type: boolean (always true)
Adds a single guild id to the list of guilds the command should be registered to
PARAMETER | TYPE | DESCRIPTION |
---|---|---|
guildId | Snowflake | The id of the guild to add1 |
Returns: GuildSlashCommandBuilder
Replace the list of guild ids with a new list
PARAMETER | TYPE | DESCRIPTION |
---|---|---|
guildIds | Array <Snowflake> | The guild ids that the command should be registered to1 |
Returns: GuildSlashCommandBuilder
Returns a new array containing global commands
PARAMETER | TYPE | DESCRIPTION |
---|---|---|
commandArray | Array <ContextMenuCommandBuilder | SlashCommandBuilder> | The array of commands made with builders |
Returns: Array <JSONEncodable <APIApplicationCommand>>
Returns a new array containing guild commands
PARAMETER | TYPE | DESCRIPTION |
---|---|---|
commandArray | Array <ContextMenuCommandBuilder | SlashCommandBuilder> | The array of commands made with builders |
guildId | Snowflake | The id of the guild to filter commands for |
Returns: Array <JSONEncodable <APIApplicationCommand>>