My Discord bot won't launch after adding a new command. What's wrong?

I’m having a hard time with my Discord bot. I built a simple bot using a guide and it worked fine with basic commands. But when I tried to introduce extra commands, the bot completely stopped running. I ended up copying and pasting segments of code, and now it won’t start at all. Every time I run the bot, I encounter syntax errors that I just can’t seem to fix. I’ve spent hours tweaking the code with no success.

Below is a reworked version of my bot code:

const DiscordModule = require('discord-api');
const logger = require('logger-tool');
const settings = require('./settings.json');

logger.initialize({ level: 'info', color: true });

const bot = new DiscordModule.Bot({
  token: settings.token,
  autoConnect: true
});

bot.on('ready', () => {
  logger.log(`Bot ${bot.username} is now online!`);
});

bot.on('message', message => {
  if (message.content.startsWith('!')) {
    const [command, ...params] = message.content.slice(1).split(' ');
    
    switch (command) {
      case 'greet':
        message.reply('Hello!');
        break;
      case 'repeat':
        message.channel.send(params.join(' '));
        break;
      // Additional commands can be added here
    }
  }
});

bot.connect();

Can anyone help me figure out why the syntax errors are occurring and how to fix them? I’m really stuck on this issue!

It seems you’re encountering a common issue when expanding bot functionality. From my experience, syntax errors often stem from inconsistent indentation or misplaced brackets. I’d suggest using a code formatter like Prettier to clean up your code structure. Additionally, the ‘discord-api’ module you’re using isn’t standard. Consider switching to the official ‘discord.js’ library, which has extensive documentation and community support. This change might resolve compatibility issues and provide you with more robust features. Remember to update your dependencies and adjust your code accordingly when making this switch. Lastly, ensure your ‘settings.json’ file is properly formatted and contains a valid bot token.

I’ve noticed that syntax errors often result from using libraries that may not be up to date with current practices. In your case, the discord-api module stands out as a potential problem because many developers now prefer the official discord.js library. My recommendation is to switch to discord.js, which is widely supported and well-documented. Begin by installing discord.js via npm, then update your require statement and client initialization accordingly, and replace bot.connect() with the appropriate login method. Also double-check that your settings.json includes a valid token and that your punctuation and indentations are correct.