Shreyas007
Shreyas007

Reputation: 374

New Discord Slash Commands

Recently, discord added support for slash commands for your own application. I read through the documentation for it, and I've tried to search for some videos (however the feature did JUST come out) but I do not understand what I actually have to do to get it working. I am using WebStorm (js, node.js). Has anyone successfully made a slash command, and if so, how?

Documentation

Upvotes: 23

Views: 43672

Answers (2)

Dwza
Dwza

Reputation: 6565

You can use the regular discord.js, by now its v12.5.1.

This is just a sample, but worked for me.

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
    client.api.applications(client.user.id).guilds(YOUR_GUILD_ID_HERE).commands.post({
        data: {
            name: "hello",
            description: "hello world command"
            // possible options here e.g. options: [{...}]
        }
    });


    client.ws.on('INTERACTION_CREATE', async interaction => {
        const command = interaction.data.name.toLowerCase();
        const args = interaction.data.options;

        if (command === 'hello'){ 
            // here you could do anything. in this sample
            // i reply with an api interaction
            client.api.interactions(interaction.id, interaction.token).callback.post({
                data: {
                    type: 4,
                    data: {
                        content: "hello world!!!"
                    }
                }
            })
        }
    });
});

client.login(token);

Of course you can have options, see documentation...

IDE won't register the new code...at least my php storm currently does'nt :)

However, its important to give the bot the correct permissions to use this type of command!

So go to Discord.com/developers, select your application, go to OAuth2 and select

application.commands

from the scope section. This should be at the bottom of the middle column. You should select bot as well and under Bot Permissionsyou set some other specific permissions. Then use that new invite link to reinvite the bot.

Without application.commands permission, the command won't work and you will receive some error like Missing Access or some similar.

IMPORTANT THINGS

  1. Use .guilds('11231...').comma to test these commands. When not using this, it takes round about 1h to get active. Using it will activate it immediately.

  2. Give the bot the correct permission. application.commands

Upvotes: 27

Shifu
Shifu

Reputation: 81

Hi I don't usually work with discord.js but I was able to find some good documentation on this. You should be able to do it like this with "client" defined.

const interactions = require("discord-slash-commands-client");

const client = new interactions.Client(
  "you unique bot token",
  "your bots user id"
);

If the client is defined as shown, then a /command should work if defined like this.

// will create a new command and log its data. 
//If a command with this name already exist will that be overwritten.
client.createCommand({
    name: "your command name",
    description: "description for this command",
  })
  .catch(console.error)
  .then(console.log);

Upvotes: 4

Related Questions