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.