My Discord bot stops responding after initial command

I’m having trouble with my Discord bot. It works fine the first time I use a command, but then it stops responding. Here’s my code:

const Discord = require('discord.js');
const bot = new Discord.Client();
const cmdPrefix = '$';

bot.once('ready', () => {
  console.log('Bot is up and running!');
});

bot.once('message', msg => {
  const cmdArgs = msg.content.slice(cmdPrefix.length).trim().split(' ');
  
  if (cmdArgs[0] === 'hello') {
    msg.channel.send('Hi there!');
  }
});

bot.login('YOUR_BOT_TOKEN_HERE');

When I type $hello, the bot replies ‘Hi there!’ as expected. But if I try the command again, nothing happens. Any ideas why it’s not responding after the first message?

I’ve encountered this issue before. The problem lies in the event listener setup; using bot.once means the listener only reacts to the first message. Changing this to bot.on ensures the bot continuously listens for subsequent messages.

It’s also advisable to check for the command prefix at the outset, for example:

if (!msg.content.startsWith(cmdPrefix)) return;

This prevents unnecessary processing of other messages and should help your bot respond reliably to commands. If issues persist, verify your bot’s permissions in the Discord server.

yo, i think i kno whats up. ur using ‘bot.once’ which only listens once. switch it to ‘bot.on’ and it’ll keep listening. also, add a check for the prefix like this:

if (!msg.content.startsWith(cmdPrefix)) return;

that should fix it. lmk if u need more help!

I’ve encountered a similar issue with my Discord bot. The problem is likely in your event listener. You’re using bot.once('message', ...) which only triggers the first time a message is received. To fix this, change it to bot.on('message', ...).

This way, your bot will continue to listen for messages after the first one. Also, don’t forget to add a check to ignore messages from other bots and messages that don’t start with your command prefix. Here’s how you could modify your code:

bot.on('message', msg => {
  if (msg.author.bot || !msg.content.startsWith(cmdPrefix)) return;

  const cmdArgs = msg.content.slice(cmdPrefix.length).trim().split(' ');
  
  if (cmdArgs[0] === 'hello') {
    msg.channel.send('Hi there!');
  }
});

This should resolve your issue and keep your bot responsive. Let me know if you need any further clarification!