简体   繁体   中英

How to break out of an else statement in node js in a discord bot

I'm creating a discord bot in Node JS using discord.js module and I want to send a predefined message only if the user sends a particular text command in a particular predefined channel on the discord server else if the user sends the command in any other channel then send a message to the same channel notifying the user to use the predefined channel for the commands. eg.

According to me the code with the bug is:

client.on('message', message => {

    //Check message channel
    if (message.channel === 'aim-reception') {

        if (message.content.startsWith(`${prefix}hi`)) {
            console.log(`${message.author} used the "!hi" command in channel ${message.channel}`);
            message.channel.send(`Hello ${message.author}!`);
        }
    } else return message.channel.send('Please Use the channel #aim-reception');
});

And here is the full code for index.js file:

const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();


// Create an event listener for new guild members
client.on('guildMemberAdd', member => {
    // Send the message to a designated channel on a server:
    const channel = member.guild.channels.find(ch => ch.name === 'member-log');
    // Do nothing if the channel wasn't found on this server
    if (!channel) return;
    // Send the message, mentioning the member
    channel.send(`Welcome to the server, ${member}`);
});


client.on('message', message => {

    //Check message channel
    if (message.channel === 'aim-reception') {

        if (message.content.startsWith(`${prefix}hi`)) {
            console.log(`${message.author} used the "!hi" command in channel ${message.channel}`);
            message.channel.send(`Hello ${message.author}!`);
        }
    } else return message.channel.send('Please Use the channel #aim-reception');
});

/**
 * The ready event is vital, it means that only _after_ this 
 * will your bot start reacting to information
 * received from Discord
 */
client.once('ready', () => {
    console.log('Bot is now connected');

});

client.login(token);

Even when the channel used is correct still it is skipping the if condition and is looping the else statement indefinitely .

A Snapshot of the error in a discord server

You need to ignore bots, you're creating an infinite loop where:

You send a message -> your bot receives it -> your bot sends a message -> your bot receives its own message -> sends a message ->...

At the start of your message handler put:

if (message.author.bot) {
  return;
}

Or if you only want to ignore your own bot:

if (message.client.user.id === message.author.id) {
  return;
}

1. Use the channel properties In the docs , you'll see that the TextChannel object has the TextChannel.name property.

When you compare message.channel === 'aim-reception' , you're comparing an object with a string, which will always return false . Use message.channel.name === 'aim-reception' instead.

2. Ignore bot messages, or at least ignore your bot's own messages

The message event will trigger for any message, including your bot's own messages, so try to ignore bot messages all together or at least just their own. Otherwise the bot will get stuck repeatedly replying to their own messages.

client.on('message', message => {
  // ignore bot messages
  if (message.author.bot) return;
  // ...
});

// or

client.on('message', message => {
  // ignore own messages
  if (message.author.id === client.user.id) return;
  // ...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM