简体   繁体   English

如何阻止在 pycord 的服务器中没有权限的命令?

[英]How can I block command who has no permission in server at pycord?

I used discord.py before, but because of discord.py developing stop, I was changing all code to pycord.之前用的是discord.py,后来因为discord.py停止开发,所以把代码都改成了pycord。 Also, I was changing bot's command from text type to slash type.另外,我正在将机器人的命令从文本类型更改为斜线类型。

But, I found that 'has_permission' was not provided at slash_command.但是,我发现 slash_command 没有提供 'has_permission'。 But I want to block using command in case who don't have enough permission at server.但是我想阻止使用命令以防在服务器上没有足够的权限。 (ex: No Perm user executed ban command) Is there any way to limit command according to Server user's permission? (ex: No Perm user executed ban command) 有没有办法根据服务器用户的权限来限制命令?

#code before rewrite(which don't have error)
@commands.has_permissions(ban_members = True)
@commands.command(name = "ban", usage = "//ban @aaa#0000")
async def ban_command(self, ctx, user_name : discord.Member, *, reason = None):
    if await Permission.check_permission(ctx, 1):
        return None
    await user_name.ban(reason = reason)
    if(reason != None):
        await ctx.reply(str(user_name) + "was banned." + "\nReason : " + str(reason), mention_author = False)
    else:
        await ctx.reply(str(user_name) + "was banned.", mention_author = False)


#code after rewrite(which has error)
@slash_command.has_permissions(ban_members = True)
@slash_command(name = "ban")
async def ban_command(self, ctx, user_name : discord.Member, *, reason = None):
    if await Permission.check_permission(ctx, 1):
        return None
    await user_name.ban(reason = reason)
    if(reason != None):
        await ctx.reply(str(user_name) + "님이 차단되셨습니다." + "\n이유 : " + str(reason), mention_author = False)
    else:
        await ctx.reply(str(user_name) + "님이 차단되셨습니다.", mention_author = False)

You can use the following if statement to check the user permissions:您可以使用以下 if 语句来检查用户权限:

if ctx.author.guild_permissions.ban_members:
    # Code for the ban command
else:
    # Respond with an Error Message

Currently discord does not have included special permissions like ban members, kick members etc for slash commands so you need to use if statements in the command itself.目前 discord 没有包含特殊权限,如禁止成员、踢成员等斜线命令,因此您需要在命令本身中使用 if 语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM