我如何检查用户是否在不和谐中扮演特定角色 - python

这应该检查特定人员是否具有静音角色

    @bot.command(pass_context=True)
    @commands.has_role("Admin")
    async def unmute(ctx, user: discord.Member):
        role = discord.utils.find(lambda r: r.name == 'Member', 
    ctx.message.server.roles)
        if user.has_role(role):
            await bot.say("{} is not muted".format(user))
        else:
            await bot.add_roles(user, role)

抛出此错误

命令引发异常:AttributeError:'Member'对象没有属性'has_role'

我不知道该怎么做,所以我将非常感谢我能提供的每一个帮助

参考方案

成员没有.has_role()方法,但是您可以使用.roles获取其所有角色的列表。

要查看用户是否具有给定角色,我们可以使用role in user.roles

    @bot.command(pass_context=True)
    @commands.has_role("Admin")
    async def unmute(ctx, user: discord.Member):
        role = discord.utils.find(lambda r: r.name == 'Member', ctx.message.server.roles)
        if role in user.roles:
            await bot.say("{} is not muted".format(user))
        else:
            await bot.add_roles(user, role)

参考文件:https://discordpy.readthedocs.io/en/latest/api.html#member

Discord.py重写所有命令的收集列表 - python

我正在尝试获取Discord机器人中所有命令的列表以进行重写。我正在使用Python 3.6编写此我试图通过执行打印命令列表print(bot.commands)这仅为我提供以下回报:{<discord.ext.commands.core.Command object at 0x00000209EE6AD4E0>, <discord.ext…

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python:图像处理可产生皱纹纸效果 - python

也许很难描述我的问题。我正在寻找Python中的算法,以在带有某些文本的白色图像上创建皱纹纸效果。我的第一个尝试是在带有文字的图像上添加一些真实的皱纹纸图像(具有透明度)。看起来不错,但副作用是文本没有真正起皱。所以我正在寻找更好的解决方案,有什么想法吗?谢谢 参考方案 除了使用透明性之外,假设您有两张相同尺寸的图像,一张在皱纹纸上明亮,一张在白色背景上有深…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Discord.py重写自定义错误 - python

我对编码非常陌生,我想知道如何在此处实现自定义错误,例如“缺少开发人员角色”: @bot.command() @commands.has_any_role("Temp.", "Owner") async def sh(ctx): await ctx.message.add_reaction(':true:50…