Skip to content

Instantly share code, notes, and snippets.

@C-EO
Forked from advaith1/discordjs-slash-commands.md
Created August 29, 2021 09:22
Show Gist options
  • Select an option

  • Save C-EO/9600ec57e1d2247f5a4a83c5dd6b592b to your computer and use it in GitHub Desktop.

Select an option

Save C-EO/9600ec57e1d2247f5a4a83c5dd6b592b to your computer and use it in GitHub Desktop.
Slash Commands in Discord.js

Slash Commands in Discord.js

These are some simple examples for using Slash Commands in discord.js.
discord.js doesn't have full support for slash commands yet (there's a wip pr) but you can still use the underlying api and websocket to use them.
Note that discord.js doesn't officially support using client.api, this is basically just a workaround until they fully release support.

Please read Discord's Slash Command docs since they have actual docs and details for slash commands; the code examples below are just how you can implement it using discord.js.

registering a command:

You only need to register each command one time. You might wanna use an eval command for this.

Send a Command object

global commands

global commands show in all authorized servers, but take up to an hour to deploy.

// if your application id and bot id are different, change client.user.id to the application id
client.api.applications(client.user.id).commands.post({data: {
    name: 'ping',
    description: 'ping pong!'
}})

guild-specific commands:

guild commands deploy immediately - use these for testing

// if your application id and bot id are different, change client.user.id to the application id
client.api.applications(client.user.id).guilds('guild id').commands.post({data: {
    name: 'ping',
    description: 'ping pong!'
}})

receiving the event:

interaction is an Interaction object

client.ws.on("INTERACTION_CREATE", async interaction => {
  
})

responding to an interaction:

send an Interaction Response object

client.api.interactions(interaction.id, interaction.token).callback.post({data: {
  type: 4,
  data: {
    content: 'hello world!'
    }
  }
})

shortlink: s.advaith.io/slashdjs

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