parsing err! ‘return’ outside of function

So, I wanted to add a unban command.
This my code so far:

if (command === ":unban") {
    if (!msg.member.hasPermission("BAN_MEMBERS")) {
        return msg.channel.send(`**${msg.author.username}**, You do not have perms to unban someone`);
    }
    
    if (!msg.guild.me.hasPermission("BAN_MEMBERS")) {
        return msg.channel.send(`**${msg.author.username}**, I do not have perms to unban someone`);
    }
    
    let userID = args[0]
    msg.guild.fetchBans().then(bans=> {
        if (bans.size == 0) return 
        let bUser = bans.find(b => b.user.id == userID);
        if (!bUser) return
        msg.guild.members.unban(bUser.user);
    });
}

when I run it it gives me this error:

parsing error: 'return' outside of function

can somebody help me out on this because im new to this stuff

Answer

You cant use a return function, with nothing to return to. Returning out of those if-cases is not possible. You could put that code in a separate function and call that, then returning would not bring an error. A simple rewrite of your code using a bool should fix the issue tho:

client.on("messageCreate", message => {
  if(message.content === "!ping"){
    message.channel.send("pong")
  } else if(message.content === '!unban') {
    let permissionToKick = true;

    if(!message.member.hasPermission("BAN_MEMBERS")) {
      permissionToKick = false
      message.channel.send(`**${message.author.username}**, You do not have perms to unban someone`)
    }

    if(!message.guild.me.hasPermission("BAN_MEMBERS")) {
      permissionToKick = false
      message.channel.send(`**${message.author.username}**, I do not have perms to unban someone`)
    }

    if(permissionToKick) {
      let userID = args[0] //args[] are not defined, and this will throw an error
        message.guild.fetchBans().then(bans=> {
        if(!bans.size === 0) {
          let bUser = bans.find(b => b.user.id === userID)
          if(bUser) {
            message.guild.members.unban(bUser.user)
          }
        }
      })
    }
  }
})

PS: I see you are using args[] in your code, but args are nowhere to be defined. I won’t write all the code for you, since Stackoverflow is not Code-writing platform. I would advise you learn the basics of JavaScript and discord.js first.

A good source to learn JavaScript is W3Schools. If you understand the basics, I advise you to read the Documentation of Discord.js or follow the Discord.js guide