【问题标题】:Privatising avatar discord.py私有化头像 discord.py
【发布时间】:2018-05-04 22:14:03
【问题描述】:

您好,我正在努力研究如何防止成员通过使用角色来获取其他成员的头像,因为您可以看到拥有私人头像角色的成员将保护他们的头像。这就是我在 .py 中编码的方式

class Avatar:
    """Returns members avatar."""

    def __init__(self, bot):
        self.bot = bot

    @commands.command(pass_context=True)
    @commands.cooldown(5, 60, commands.BucketType.user)
    async def avatar(self, context, member: discord.Member):
        """Returns members avatar."""

        role = discord.utils.get(member.server.roles, name="Private Avatar")
        author = context.message.author.mention
        mention = member.mention

        if role in member.roles:
            pa = "This members avatar is private"
            await self.bot.say(pa)
            return

        avatar = "{0} here is {1}'s avatar"

        u = member.avatar_url
        url = process_avatar(u)
        embed = discord.Embed(description=avatar.format(author, mention), colour=discord.Colour.blue())
        embed.set_image(url=u)
        await self.bot.say(embed=embed)

我正在努力的代码行是这一行

 if role in member.roles:
            pa = "This members avatar is private"
            await self.bot.say(pa)
            return

这应该基本上输出消息“此成员头像是私人的”而不检索头像,但它仍然会检索带有消息的头像。

非常感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    你把它弄得太复杂了。只需使用else:,就像这样:

    class Avatar:
        """Returns members avatar."""
    
        def __init__(self, bot):
            self.bot = bot
    
        @commands.command(pass_context=True)
        @commands.cooldown(5, 60, commands.BucketType.user)
        async def avatar(self, context, member: discord.Member):
            """Returns members avatar."""
            
            role = discord.utils.get(member.server.roles, name="Private Avatar")
            author = context.message.author.mention
            mention = member.mention
            
            if role in member.roles:
                pa = "This members avatar is private"
                await self.bot.say(pa)
            else:
    		
                avatar = "{0} here is {1}'s avatar"
    
                u = member.avatar_url
                url = process_avatar(u)
                embed = discord.Embed(description=avatar.format(author, mention), colour=discord.Colour.blue())
                embed.set_image(url=u)
                await self.bot.say(embed=embed)
            

    P.S.:理论上,您的代码应该可以工作。除非有什么我没有看错的地方,否则就是这样。我还必须注意,仅仅因为机器人不会获取它,并不意味着客户端不能。头像是公开的。

    【讨论】:

    • 谢谢,我试过了,但仍然可以获取我不想做的头像。我不确定这里出了什么问题。
    • 这意味着您的支票运行不正确。也许尝试在不同的地方添加一些print 语句来检查您的搜索角色是否被正确找到或者该成员是否与您的搜索角色具有相同的角色。例如print(role) 应该返回 <discord.role.Role id="..." ...> 如果它找到它,如果它打印 None 则有问题。
    • 我发现这是一个角色 ID 问题。感谢您的帮助
    • 很高兴你知道了@troxie
    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2018-09-19
    • 2020-11-27
    • 1970-01-01
    • 2021-02-08
    相关资源
    最近更新 更多