2

I wanted to add a kick command to my discord bot, which uses the await function.The only way i know to async run is

client.on("message", async (message) => {

however, I cannot do this in a command file inside the commands folder My kick command

const { MessageEmbed } = require("discord.js");

module.exports = {
  name: "kick",
  description: "kicks a member",
  execute(client, args) {

    if (!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send("Invalid Permissions")
    let member = message.mentions.members.first()
    if(!member)
      return message.reply("Please mention a valid member of this server");
    if(!member.kickable) 
      return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
    
    let reason = args.slice(1).join(' ');
    if(!reason) reason = "No reason provided";
    
    await member.kick(reason)
      .catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
    message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`)
    }}

Sorry if this seems like a stupid question, Im pretty new to coding

2
  • If possible, could you supply some more information about how your commands work. What code is being used to interpret that command in your main file? Commented Oct 14, 2020 at 18:36
  • 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); };
    – peter 23
    Commented Oct 14, 2020 at 18:42

1 Answer 1

3

Try changing your code to:

module.exports = {
  name: "kick",
  description: "kicks a member",
  // vvv THIS LINE CHANGED vvv
  // vvv THIS LINE CHANGED vvv
  // vvv THIS LINE CHANGED vvv
  execute: async (client, args) => {

    if (!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send("Invalid Permissions")
    let member = message.mentions.members.first()
    if(!member)
      return message.reply("Please mention a valid member of this server");
    if(!member.kickable) 
      return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
    
    let reason = args.slice(1).join(' ');
    if(!reason) reason = "No reason provided";
    
    await member.kick(reason)
      .catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
    message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`)
    }}

Specific Change: execute(client, args) { becomes execute: async (client, args) => {

Essentially what that does is it says the function is async, vs your code is just declaring execute as a normal (sync) function. Also since I'm not sure if you can declare an async function with normal function syntax, I changed it to ES6 arrow function syntax: eg. execute() {} to execute: () => {} You can read more about arrow functions here

1
  • ah yes, I tried to do this, but I didnt know that I was supposed to put the arrow, thank you so much
    – peter 23
    Commented Oct 15, 2020 at 11:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.