我对我的Discord机器人踢系统有一个问题
我需要做到这一点,不是每个人都可以踢成员,而只有拥有权限的人 这是我的踢系统 -
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 跳过一步并在某种程度上简化代码。
如上所述,您将需要使用
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 theMANAGE_MEMBERS
permission, or whatever other permission you want to check for. (SeePermissions.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).为了仅制作mods,我建议您使用
message.member.member.permissions.has()
,该> 允许bot检查运行命令的人是否在行会或不是。然后,要返回,如果该人没有许可,则可以在这样的语句中使用它:当然,您可以随时添加
messageCreate
事件a用户将添加在您的命令顶部。因此,事件文件将具有:
然后,您可以在名称或描述中添加命令
用户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 anif
statement like that: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:
then you can just add in your command under name or description
userPermissions: ['KICK_MEMBERS'],
This is the easiest way to handle user permissions