Home > Mobile >  Discord.js Can't ban member by message author
Discord.js Can't ban member by message author

Time:11-22

I want the bot to ban member.

Code:

  let member = message.guild.members.get(message.author);
  if(message.content === message.content && message.channel.id === id)
    member.ban("reason")
    message.channel.send("x") 

So basically, whenever member sends message in specified channel, he will get ban. However, it does not ban. I tried to do it like message.author.id and so on but not successfully. Also, as I said it does not ban the member, but it sends the "x" message to the channel. I want to fix the ban problem and that it would send the "x" message as soon as he gets ban.

CodePudding user response:

  1. <GuildMemberManager>.get doesn't exist. You should use its fetch method, that returns a <GuildMember> that you can ban using its ban method. This method returns a Promise so you need to await it. So replace let member = message.guild.members.get(message.author); with let member = await message.guild.members.fetch(message.author.id);

  2. The expression message.content === message.content shouldn't be a condition as it always returns true.

  let member = await message.guild.members.fetch(message.author.id);

  if(message.channel.id === id)
    member.ban("reason")
    message.channel.send("x") 

Also don't forget to handle the Promise rejection of the fetch, ban and send methods.

CodePudding user response:

Can you try this ?

 if (!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send(perms)
        if (user) {
            const member = message.guild.members.resolve(user);
            if (member) {
                member
                    .ban({
                        reason: 'Reason for ban',
                    })
  • Related