Skip to content

More builders to help manage application commands for applications and guilds

Notifications You must be signed in to change notification settings

edocsil47/guildbuilders

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

guildbuilders

More builders to help manage application commands for applications and guilds

Contents:

  1. Installation
  2. Example usage
  3. Classes
    3.1. GuildContextMenuCommandBuilder
    3.2. GuildSlashCommandBuilder
  4. Functions
    4.1. filterGlobalCommands
    4.2. filterGuildCommands

Installation

npm i .\<local directory>\

Discord.js and its dependencies are required

Example usage

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}.`))

Classes

GuildContextMenuCommandBuilder extends ContextMenuCommandBuilder

Constructor

new GuildContextMenuCommandBuilder()

Properties

.guildIds

All of the guild ids that the command should be registered to

Type: Array <Snowflake>

.isGuildCommand

Whether the application command should be registered to guilds or the application globally

Type: boolean (always true)

Methods

.addGuildId(guildId)

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

.setGuildIds(guildIds)

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

Constructor

new GuildSlashCommandBuilder()

Properties

.guildIds

All of the guild ids that the command should be registered to

Type: Array <Snowflake>

.isGuildCommand

Whether the application command should be registered to guilds or the application globally

Type: boolean (always true)

Methods

.addGuildId(guildId)

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

.setGuildIds(guildIds)

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

Functions

filterGlobalCommands(commandArray)

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>>

filterGuildCommands(commandArray, guildId)

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>>

Footnotes

  1. Adding a guild does not update the commands in Discord. They still need to be deployed 2 3 4

About

More builders to help manage application commands for applications and guilds

Resources

Stars

Watchers

Forks

Packages

No packages published