JetSamster
JetSamster

Reputation: 5

There is an error with async being the problem

It comes up an async error. The code I am using is below. I have even added async message yet there is still an error? I have searched it up yet there have been no answers.



    let args = message.content.slice(prefix.length).split(" ");
    
    switch(args[0]){
        case 'ban':
        if(!message.member.hasPermission("BAN_MEMBERS")) {
          return message.channel.send("You cannot use this command.")
        }
        member = message.mentions.members.first(); 
        if (!member) {
          message.react("❌");
          return message.channel.send("Missing arguments. -ban @user Reason");
        } 

        reason = args.slice(1).join(" "); 
        if (!reason) reason = "Please provide a reason."; 

        await member
        .ban(reason) 
        message.react("✔️");
        embed = new MessageEmbed()
        .setColor("RED")
        .setTitle("You banned them.")
        .setDescription(`${member} is banned forever. He he`)
        .addFields(
          {name: 'reason', value: reason}
        )
        .setFooter("You banned someone.");
        message.channel.send(embed);
      };
});

Upvotes: 0

Views: 129

Answers (1)

Yankue
Yankue

Reputation: 378

After member.ban() there should be a semi-colon and ideally a new line. You've not shown the function it is enclosed in, but it must be an async function, as a normal function cannot contain the word await. To make an async function, rather than doing function runCommand() { }, simply you instead do async function runCommand() { }

This is because an async function, as the name asynchronous suggests, runs asynchronous to everything else, so at the same time as. A normal function runs on its own, so if you put a wait in a normal function, it would cause the whole bot to sort of freeze while it waits for it.

Upvotes: 1

Related Questions