Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  114] [ 6]  / answers: 1 / hits: 6096  / 3 Years ago, sun, march 14, 2021, 12:00:00

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);

More From » node.js

 Answers
7

This answer is a outdated!


When it was accepted Discord haven't introduced truly /slash commands. So use the answer below, 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.');
}
})

[#1652] Monday, March 8, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austonjuancarlosb

Total Points: 238
Total Questions: 89
Total Answers: 99

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;