Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Discord Slash Commands

Tags:

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

like image 203
Shreyas007 Avatar asked Dec 22 '20 01:12

Shreyas007


People also ask

Why can't I use slash commands in Discord?

If your app has no slash commands, that means your developer hasn't migrated to them yet. You can visit their support server or contact them to let them know - they might be juggling a lot, life can get busy!

Does Discord JS have slash commands?

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.


Video Answer


2 Answers

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

like image 177
Dwza Avatar answered Oct 05 '22 07:10

Dwza


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); 
like image 40
Shifu Avatar answered Oct 05 '22 08:10

Shifu