Skip to content

Instantly share code, notes, and snippets.

@advaith1
Last active April 20, 2024 05:39
Show Gist options
  • Save advaith1/287e69c3347ef5165c0dbde00aa305d2 to your computer and use it in GitHub Desktop.
Save advaith1/287e69c3347ef5165c0dbde00aa305d2 to your computer and use it in GitHub Desktop.
Slash Commands in Discord.js

Discord.js slash command support has been merged!

The discord.js interactions PR has been merged, and d.js master (v13 dev) now has proper support for slash commands!

Official resources:

For help with official slash command support, go to the Discord.js server and ask in #djs-master-branch.

If you are using client.api you should switch over to official support. The following legacy information is not recommended or supported for use.


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 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. Do not ask for help with client.api in the discord.js server.

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.

Note that slash commands won't show in a server unless that server has authorized it with the applications.commands oauth2 scope (not just the bot scope).

This does not require a discord.js update! It should work as long as you're using a modern version (anything v12 would probably work, obviously v12.5.1/latest is recommended)

Alternatively, you can manage commands and handle interactions with slash-create.

Registering a Command:

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

Alternatively, instead of using discord.js to create the command, you might want to use a UI tool such as Postman, or create them in your code with discord-slash-commands

Send a Command object

For help creating the command object json, try https://rauf.wtf/slash

if your application id and bot id are different, change client.user.id to the application id

Global Commands

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

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

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 => {
  // do stuff and respond here
})

Responding To An Interaction:

this goes inside the "receiving the event" block.
send an Interaction Response object

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

Sending a Followup Message

this also goes inside the "receiving the event" block.
see Webhook#send docs

new Discord.WebhookClient(client.user.id, interaction.token).send('hello world')

shortlink: s.advaith.io/slashdjs

@m3hari
Copy link

m3hari commented Aug 25, 2021

For me, typescript was complaining when using client.api . Here is how I respond to interaction (ephemeral) directly calling the webhook

await got.post(`https://discord.com/api/v8/interactions/${interaction.id}/${interaction.token}/callback`, {
      json: { type: 4, data: { content: '"hello world ...", flags: 64 } }
    })

@spifory
Copy link

spifory commented Sep 7, 2021

Can you use the "Only visible to you" flag with a message embed?

you are able to do that in djs v13 using the emphermal thingy

@spifory
Copy link

spifory commented Sep 7, 2021

client.api.applications(client.user.id).commands.get()

@advaith i did the

client.api.applications(client.user.id).commands.get()

and all i get is

Promise { <pending> }

but nothing turns up in the console, even my eval command nothing happens

@promise
Copy link

promise commented Sep 7, 2021

client.api.applications(client.user.id).commands.get()

@.advaith i did the

client.api.applications(client.user.id).commands.get()

and all i get is

Promise { <pending> }

but nothing turns up in the console, even my eval command nothing happens

hey @shamrockyt , you need to await the promise. You can do client.api.applications(client.user.id).commands.get().then(console.log) to send it to the console. If you're in an async function then you can do const cmds = await client.api.applications(...

@spifory
Copy link

spifory commented Sep 8, 2021

client.api.applications(client.user.id).commands.get()

@.advaith i did the

client.api.applications(client.user.id).commands.get()

and all i get is

Promise { <pending> }

but nothing turns up in the console, even my eval command nothing happens

hey @shamrockyt , you need to await the promise. You can do client.api.applications(client.user.id).commands.get().then(console.log) to send it to the console. If you're in an async function then you can do const cmds = await client.api.applications(...

ohhhh @promise thank you by looking at the code it makes sense so thanks!

@valorantwikibot
Copy link

Can you use the "Only visible to you" flag with a message embed?

@CRAFTIL yes you can do it now, just mark empheral as true

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