bananapie
bananapie

Reputation: 39

Slash Commands - Discord.js

I am getting an error when I am trying to run: (node:9164) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'applications' of undefined

Here is my code:

const discord = require('discord.js');
const client = new discord.Client();
const guildId = '820368493017825333';
client.on('ready', async () => {
    console.log('ready');

    const commands = await client.api.
    applications(client.user.id)
    .guilds(guildId)
    .commands.get();
    console.log(commands);
});

client.login(require(`./config.json`).Token);

Upvotes: 2

Views: 6662

Answers (2)

AlexZeDim
AlexZeDim

Reputation: 4352

This answer is a outdated!

When it was accepted Discord hadn't yet truly introduced /slash commands. So use the other answer, if you want to integrate or migrate to newest version of Discord.js

Well, the answer is pretty simple here. According to Discord.js docs, Class Client doesn't have api property. That's why you have the undefined error.

It seems like the tutorial that you are looking at is a bit outdated, or probably the tutor adds this property manually because Discord.js have relevant classes, like Application and ClientApplication but I still don't see an api property there as well.

If you are looking for a good guide, I might recommend you this one from the official Discord recommendation page.

If you want to implement commands to your Discord bot with slash support, just add the following code, after ready stage.

const prefix = '/'

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping') {
        message.channel.send('Pong.');
    }
})

Upvotes: 0

Cannicide
Cannicide

Reputation: 4520

Issues With the Accepted Answer

The accepted answer is incorrect in several ways. I'll walk through the inaccuracies in that answer and highlight the more likely causes of this problem, for the sake of anyone that may stumble upon this question in the future (and would've been misled by the accepted answer into believing that Slash Commands cannot be implemented in discord.js).

Well, the answer is pretty simple here. According to Discord.js docs, Class Client doesn't have api property. That's why you have the undefined error.

Incorrect. The Client class does have an api property. Or rather, it inherits the api property from the BaseClient class that it extends. This can be found in the source code of BaseClient. It is true that this is not documented in the discord.js docs. That is intentional, as the api property is intended to be a private property, more for discord.js' own use than for general use. You may notice in the source code that the property is annotated with @private, which usually indicates that it will not appear in the docs. There are many such private properties and methods that exist in discord.js classes, which are undocumented but are usable in your own code.

It seems like the tutorial that you are looking at is a bit outdated, or probably the tutor adds this property manually because Discord.js have relevant classes, like Application and ClientApplication but I still don't see an api property there as well.

The tutorial that the OP was going off of was actually more up-to-date than the tutorials posted and used by the accepted answer. The Application and ClientApplication classes are not at all relevant, as neither can access Slash Commands. Nor did hundreds of different tutorials each implement their own api property that all work in exactly the same way; they were all using the api property included in the latest versions of discord.js.

If you want to implement commands to your Discord bot with slash support, just add the following code, after ready stage.

The accepted answer misunderstood what 'Slash Commands' are, and provided code simply for creating a command with a slash for a prefix. That is not what the Slash Command system is. Slash Commands allow you to do things such as documenting, autocompleting, and validating commands and command arguments that users are typing in, in real-time while they are entering their input.

Not it shouldn't. Actually, the Discord.js lib is updated more often, the [YouTube] creators do it with their videos. I have already placed in my answer, a relevant guide made by the Discord.js community.

Yes it should. Hundreds of tutorials used the same code as each other, containing the api property, in instructing developers on how to work with Slash Commands in unmodified discord.js. I am not sure what exactly was meant by this comment.

If you look at the actual source code of discord.js, you'll find that the latest versions use the client's api property several times internally, usually in methods that directly query the Discord API for information (such as .fetch() methods). If the api property is undefined and you are using the latest version of discord.js, then much of your bot would not be working properly. So the latest client class not having an api property is not the main issue, which leads us to what the main issue really is.

So What Is the Real Issue?

There truly isn't enough context provided in the question to know for sure what exactly was causing the issue in the question. However, we can narrow the cause down to a few potential suspects, especially given the information aforementioned. Double check these to ensure they are not causing your problem:

  1. Discord.js version. The api property does not exist for versions older than v12. Make sure you are using the latest version of discord.js. This is most likely the cause of the issue.
  2. Missing access. You need to give your bot the application.commands scope when generating its invite link, otherwise you cannot interact with or create Slash Commands. This shouldn't really cause the api property to be undefined and should give you a different error, but it's worth double-checking.

If working with Slash Commands in simple discord.js is still not working for you even after double-checking both of these potential issues, you may want to consider an alternate (and somewhat simpler) approach to implementing Slash Commands: the discord-slash-commands-client module.

You would initialize this module like so:

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

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

Then to get a list of all existing Slash Commands, as the code in this question is attempting to do, all you would need to do with this module is:

let commands = await iclient.getCommands();

A single, clean line. As simple as it gets. The only downside to this alternate approach is that this module may not stay up-to-date as reliably as discord.js itself does. However, it would certainly be helpful if you are not able to figure out how to get Slash Commands working in discord.js itself.

If you need more help on this or want to see a more complete implementation of either approach, this question has several good, working examples on how to get Slash Commands code working properly on your bot.

Upvotes: 9

Related Questions