-3

I'm beginning to write a discord bot with the Discord.js library (version 11.5.1). Promises returned by methods don't work as expected because then() and catch() callbacks are both called if it's successful.

I'm using nodejs version 11.15.0.

In this example, I send a message in the test-bot channel when the bot in logged.

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

client.on('ready', () => {
    const guild = client.guilds.find(
        guild => guild.name === 'test');
    const channel = guild.channels.find(
        ch => ch.name === 'test-bot');
    if (!channel) {
        console.error('no channel found');
        return ;
    }
    channel.send('heeeello')
        .then(console.log('cool'))
        .catch(console.error('grrr'));
});
client.login(auth.token);

The message is well sended on the discord channel and the console output is:

cool
grrr

but I don't expect grrr in the output.

rems14
  • 3
  • 1

1 Answers1

5

The then & catch should receive a callback function, you are calling console.log.

channel.send('heeeello')
.then(() => console.log('cool') )
.catch(() => console.log('grrr') );
Tarek Essam
  • 3,602
  • 2
  • 12
  • 21