【问题标题】:How to check if owner in discord.py如何检查discord.py中的所有者
【发布时间】:2021-07-06 05:56:31
【问题描述】:

我正在尝试将此命令设置为只有所有者才能运行它。无论如何要检查最高角色或服务器的创建者吗?

我尝试了 '@commands.is_owner()' 但它只检查某人是否是机器人的所有者。

【问题讨论】:

  • 您可以通过 discord 对象获取 discord 所有者。只需检查这是否也是作者。
  • 你可以使用@commands.has_role(role_id)装饰器。这需要用户具有指定的角色才能运行命令。

标签: python python-3.x discord discord.py


【解决方案1】:

Guild.owner 存在,所以这里是它的使用示例!

from discord.ext import commands


bot = commands.Bot(command_prefix="your_prefix")

# example use with custom decorator
def guild_owner_only():
    async def predicate(ctx):
        return ctx.author == ctx.guild.owner  # checks if author is the owner

    return commands.check(predicate)

@bot.command()
@guild_owner_only()
async def ...

【讨论】:

    【解决方案2】:

    你可以使用装饰器:

    @commands.has_role("RoleName")
    

    例子:

    import discord
    from discord.ext import commands
    from discord.ext.commands.core import command
    
    @bot.command
    @commands.has_role("RoleName")
    async def ...(ctx)
        await ctx.send("your message")
    

    让我知道它是否有效!

    编辑:您也可以导入所有内容:

    import discord
    from discord import user
    from discord.ext import commands
    from discord.ext.commands.core import command
    from discord.member import Member
    from discord.message import Message
    from discord.user import User
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-10
      • 2021-05-20
      • 2021-04-30
      • 2020-10-06
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 2017-12-04
      相关资源
      最近更新 更多