Trying to build node.js discord bot, and i get SyntaxError: illlegal return statement

this is my code for my discord bot: the part that has the error is-if (!client.commands.has(command)) return;
im following this tutorial= Tutorial i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message i need to put this in to post this message

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
 
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require("./config.json")

const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

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

  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const command = args.shift().toLowerCase();

  if (message.content === `${prefix}serverinfo`) {
    message.channel.send(message.guild.name)
    message.channel.send(`Total Members: ${message.guild.memberCount}`)
  } else if (message.content === `${prefix}me`) {
    message.channel.send(`Username: ${message.author.username}`)
    message.channel.send(`ID: ${message.author.id}`)
  } else if (message.content === `${prefix}boi`) {
    message.channel.send('BOI')
  }
});

if (!client.commands.has(command)) return;

const command = client.commands.get(command);
try {
  command.execute(message, args);
} catch(error) {
  console.error(error);
  message.reply("There was an issue executing that command!")
}
client.login(token);

5 thoughts on “Trying to build node.js discord bot, and i get SyntaxError: illlegal return statement”

  1. You can’t return in the top scope, return must be inside a function.

    You may either put the logic inside an if statement

    if (client.commands.has(command)) {
      const command = client.commands.get(command);
      try {
        command.execute(message, args);
      } catch (error) {
        console.error(error);
        message.reply("There was an issue executing that command!")
      }
      client.login(token);
    }
    

    Or wrap the logic inside a function, an IIFE may be a good choice:

    (() => {
      if (!client.commands.has(command)) return;
    
      const command = client.commands.get(command);
      try {
        command.execute(message, args);
      } catch (error) {
        console.error(error);
        message.reply("There was an issue executing that command!")
      }
      client.login(token);
    })();
    
    Reply

Leave a Comment