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

@Dragonizedpizza
Copy link

discordjs/discord.js#5448

It's done.

We all know that, it's written on top of the gist and I have the repo on follow
I just wanna know what I did wrong for informational purposes, I'm already using djs master

@Tilier
Copy link

Tilier commented May 10, 2021

@AndrewDaGuy use client.api.applications(client.user.id).commands.get() to get the command list with IDs, then client.api.applications(client.user.id).commands('id').delete() to delete a command

@advaith1 I teied using the .get method and end the message, but supposedly it's an empty message, even though I do have commands set up.

@YodaLightsabr
Copy link

@YodaLightsabr no

Update: A week ago they fixed it so now you can set private messages with embeds

Copy link

ghost commented May 12, 2021

How can I get the data of the channel where the slash command is used? Like message.channel

@RadSton
Copy link

RadSton commented May 12, 2021

This worked for me client.channels.fetch(interaction.channel_id)

Copy link

ghost commented May 12, 2021

oh thank you, well how do i make my bot send a message that only the user using the command can see? only you can see this message

@Dragonizedpizza
Copy link

previous response
@earearray

@YodaLightsabr
Copy link

oh thank you, well how do i make my bot send a message that only the user using the command can see? only you can see this message

https://gist.github.com/advaith1/287e69c3347ef5165c0dbde00aa305d2#gistcomment-3626429
https://gist.github.com/advaith1/287e69c3347ef5165c0dbde00aa305d2#gistcomment-3571380
https://gist.github.com/advaith1/287e69c3347ef5165c0dbde00aa305d2#gistcomment-3707038

Have you tried reading the comments or using Ctrl + F to find on page?

Copy link

ghost commented May 16, 2021

previous response
@earearray

thank u

Copy link

ghost commented May 16, 2021

oh thank you, well how do i make my bot send a message that only the user using the command can see? only you can see this message

https://gist.github.com/advaith1/287e69c3347ef5165c0dbde00aa305d2#gistcomment-3626429
https://gist.github.com/advaith1/287e69c3347ef5165c0dbde00aa305d2#gistcomment-3571380
https://gist.github.com/advaith1/287e69c3347ef5165c0dbde00aa305d2#gistcomment-3707038

Have you tried reading the comments or using Ctrl + F to find on page?

thank u

@iam-joel
Copy link

I updated my code for my bot and I'm facing an issue where it shows duplicate slash commands. Anyone else facing this issue? I think it might be because of how discord stores cache. I'm not sure. Would appreciate any help. Thanks.

@YodaLightsabr
Copy link

I updated my code for my bot and I'm facing an issue where it shows duplicate slash commands. Anyone else facing this issue? I think it might be because of how discord stores cache. I'm not sure. Would appreciate any help. Thanks.

If you have guild and global commands it will show as two commands, even if they have the same options and name and description.

@sudhxnva
Copy link

Hey, attaching images to embeds that are sent back as a part of an interaction doesn't seem to working. Is this a limitation of slash commands as of now?

@Dragonizedpizza
Copy link

it isn't, djs's master branch allows it. I'm not sure why we can't send it via advaith's method though.

@YodaLightsabr
Copy link

Hey, attaching images to embeds that are sent back as a part of an interaction doesn't seem to working. Is this a limitation of slash commands as of now?

I haven't had any luck with sending a picture file and referencing it from the embed either.

@YodaLightsabr
Copy link

I know I can send messages with the raw data, but how could I convert any object TextChannel#send accepts into an object that the Disord API would accept? How does TextChannel#send do it?

@gabitojlsalj
Copy link

how can I add multiple responses for each different slash command, it only replies the same 'hello world' to each slash command I add

@bobjoerules
Copy link

client.ws.on('INTERACTION_CREATE', async interaction => {
client.api.interactions(interaction.id, interaction.token).callback.post({data: {
type: 4,
data: {
content: 'test'
}
}})
})

when you do any / command it will send test how would I make it so I can have multiple / commands with different responses?

@Henry-Hiles
Copy link

This is amazing!!!

@SHUSTRIK-Milan
Copy link

How can I edit a sent user message using the slash command, as it is done using the standard /me command?

PATCH /webhooks/<application_id>/<interaction_token>/messages/@original sending me error "Unknown Webhooks"

@advaith1
Copy link
Author

advaith1 commented Jul 5, 2021

@SHUSTRIK-Milan you cannot send messages from the user or edit messages sent by the user; /me is a built-in client-side command, not an application command

@SHUSTRIK-Milan
Copy link

@SHUSTRIK-Milan you cannot send messages from the user or edit messages sent by the user; /me is a built-in client-side command, not an application command

It is a pity, that there are no such function 😢

@FluffyDev1
Copy link

FluffyDev1 commented Aug 6, 2021

Argument of type '"INTERACTION_CREATE"' is not assignable to parameter of type 'WSEventType'
Sadly this is a thing when using Typescript!

('INTERACTION_CREATE' as WSEventType)

Import WSEventType from discord.js

@m3hari
Copy link

m3hari commented Aug 25, 2021

oh thank you, well how do i make my bot send a message that only the user using the command can see? only you can see this message

add flags:64 in the response
Screen Shot 2021-08-25 at 15 30 09

@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