我怎样才能有两个相互对抗的命令装饰器?

How can I have two command decorators that go against each other?(我怎样才能有两个相互对抗的命令装饰器?)
本文介绍了我怎样才能有两个相互对抗的命令装饰器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个命令可以删除用户输入的指定数量的消息.我希望只有我和具有管理员角色的人才能访问此命令.我以前用 if 语句实现过这个,它工作得很好.但是,现在我正在尝试使用命令装饰器来做同样的事情,它只允许管理员使用命令 - 而不是我.这是我正在使用的代码:

I have a command that deletes a specified number of messages entered in by the user. I want this command to be accessible only to me and those with the administrator role. I had implemented this before with if-statements, and it was working perfectly fine. However, now I'm trying to use command decorators to do the same, and it only lets administrators use the command - not me. Here's the code I'm working with:

@bot.command(description="clears entered amount of messages")
@commands.is_owner() # checks if user is owner
@commands.has_permissions(administrator=True) # checks if user is admin
async def delete(ctx, amount : int):
    await ctx.channel.purge(limit = amount + 1)

我认为 @commands.has_permissions(administrator=True) 阻止了我使用该命令,因为我不是其中一台服务器的管理员.我试过切换他们的订单,is_owner() 检查低于 has_permissions() 检查;尽管如此,它仍然不允许我使用该命令.如何使用装饰器克服这个问题?

The @commands.has_permissions(administrator=True), I think, is blocking me from using the command, since I'm not an administrator in one of the servers. I've tried switching their orders, with the is_owner() check being below the has_permissions() check; nonetheless, it still doesn't allow me to use the command. How can I overcome this using decorators?

推荐答案

您可以创建一个自定义装饰器来检查您是机器人的所有者还是管理员

You can create a custom decorator that will check if you're either the owner of the bot, or you are an admin

def owner_or_admin():
    def predicate(ctx):
        owner = ctx.author.id == bot.owner_id # Comparing the author of the message with the owner of the bot
        perms = ctx.author.guild_permissions.administrator # Checking for admin perms
        return owner or perms
    return commands.check(predicate)


@bot.command(description="clears entered amount of messages")
@owner_or_admin()
async def delete(ctx, amount : int):
    await ctx.channel.purge(limit = amount + 1)

我刚刚发现 commands.check_any - 检查是否有任何通过的检查将通过,即使用逻辑 OR.

@bot.command()
@commands.check_any(commands.is_owner(), commands.has_permissions(administrator=True))
async def delete(ctx, amount: int):
    await ctx.channel.purge(limit=amount + 1)

这篇关于我怎样才能有两个相互对抗的命令装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I make a list of all members in a discord server using discord.py?(如何使用 discord.py 列出不和谐服务器中的所有成员?)
how to change discord.py bot activity(如何更改 discord.py 机器人活动)
Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)
Add button components to a message (discord.py)(将按钮组件添加到消息(discord.py))
on_message() and @bot.command issue(on_message() 和@bot.command 问题)
How to edit a message in discord.py(如何在 discord.py 中编辑消息)