我对我的Discord机器人踢系统有一个问题

发布于 2025-02-07 13:16:34 字数 910 浏览 1 评论 0 原文

我需要做到这一点,不是每个人都可以踢成员,而只有拥有权限的人 这是我的踢系统 -

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!");
      }
    }
  });

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!");
      }
    }
  });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半仙 2025-02-14 13:16:34

您可以使用 跳过一步并在某种程度上简化代码。

如上所述,您将需要使用 Member.permissions.has() 检查成员是否具有 manage_members 许可,或您想要检查的任何其他许可。 (请参阅

另外,如果可能的话,请考虑使用基于消息的命令。对于一个小型的个人机器人,这并不是真正的问题,但是Discord确实是在推动新的 Interactions (又称Slash命令)系统,您可以阅读有关此处(官方,discord.com)。

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).

泅渡 2025-02-14 13:16:34

为了仅制作mods,我建议您使用 message.member.member.permissions.has(),该> 允许bot检查运行命令的人是否在行会或不是。然后,要返回,如果该人没有许可,则可以在这样的语句中使用它:

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

当然,您可以随时添加 messageCreate 事件a用户将添加在您的命令顶部。

因此,事件文件将具有:

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

然后,您可以在名称或描述中添加命令用户permissions:['kick_members'],
这是处理用户权限的最简单方法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文