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);
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.');
}
})