-2

I need to make that not everyone can kick members, but only who have permissions Here is my kick system-

client.on('message', message => {

    if (!message.guild) return;


    if (message.content.startsWith('+kick')) {

      const user = message.mentions.users.first();

      if (user) {

        const member = message.guild.members.resolve(user);

        if (member) {

          member
            .kick({
              reason: 'They were bad!',
            })
            .then(() => {

              message.channel.send(`Successfully kicked ${user.tag}`);
            })
            .catch(err => {

              message.channel.send('I was unable to kick the member');

              console.error(err);
            });
        } else {

          message.channel.send("That user isn't in this guild!");
        }
      } else {

        message.channel.send("You didn't mention the user to kick!");
      }
    }
  });
j08691
  • 204,283
  • 31
  • 260
  • 272
Almis
  • 13
  • 1
  • Use `member.permissions.has()`. From docs: https://discordjs.guide/popular-topics/permissions.html#checking-member-permissions – yes sir Jun 14 '22 at 19:31

2 Answers2

1

Welcome to StackOverflow! And, from the look of things, the beginning of your bot-making journey.

You can use message.mentions.members.first() to skip a step and simplify your code somewhat.

As mentioned above, you'll want to use member.permissions.has() to check if a member has the MANAGE_MEMBERS permission, or whatever other permission you want to check for. (See Permissions.FLAGS)

Also, consider moving away from using message-based commands, if possible. For a small personal bot, it's not really an issue, but Discord is really pushing the new Interactions (aka slash commands) system, which you can read more about here (discord.js guide) and here (official, discord.com).

Delta
  • 444
  • 1
  • 4
  • 14
-2

in order to make only mods use a command I recommend you using message.member.permissions.has() which allows the bot to check if the person who ran the command has a certain permission in the guild or not. Then to return, if the person doesn't have permission, you can use it in an if statement like that:

if (!message.member.permissions.has("KICK_MEMBERS")) {
    return message.reply({content: `You need permissions to use command`})
}

And of course you can always add in your messageCreate event a userPermissions to add on the top of your command.

So the event file would have:

if (!message.member.permissions.has(<command>.userPermissions || [])) return message.channel.send({content: 'You need permissions to run this command'})

then you can just add in your command under name or description userPermissions: ['KICK_MEMBERS'], This is the easiest way to handle user permissions

Christy
  • 173
  • 2
  • 13
  • 1
    Add details to your answer and explain how it works, don't just dump code – Elitezen Jun 15 '22 at 01:12
  • While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Saeed Zhiany Jun 15 '22 at 03:25
  • is this better? – Christy Jun 15 '22 at 09:28