尝试显示可提及角色的问题

时间:2019-03-25 00:11:26

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

嗨,我在尝试显示一个符号时遇到问题,该符号表示可以在Discord中提及的角色。我目前正在使用discord.py,他们使用的是role.mentionable,它是布尔值https://discordpy.readthedocs.io/en/rewrite/api.html#discord.Role.mentionable

我要完成但未能完成的工作是能够在可提及的角色前面设置add:bell:,而在:nobell:之前设置不可用的角色。

我收到错误AttributeError: 'str' object has no attribute 'mentionable'

这是我正在使用的代码:

@commands.command(pass_context=True, no_pm=True, name='list', aliases=['roles', 'role'])
async def _list(self, ctx):
    """List of all available roles """
    guild = ctx.message.guild
    author = ctx.message.author
    botroom = self.bot.get_channel(555844758778544160) 
    intros = self.bot.get_channel(485381365366390796)
     #--- Role list Categories ---"


    if ctx.message.channel == intros:
        pass
    else:
        if ctx.message.channel == botroom:
            title = '**Hey {}, here is a categorised list of roles you can add:**'.format(author.display_name)
            embed = discord.Embed(title=title.format(), colour=0x0080c0)
            embed.add_field(name="\n**__Notifications__**", value="roles with a :bell: at the beginning of them are @mentionable - when applied you may recieve notifications.\n\n", inline=False)
            embed.set_footer(text="Tip: to add a role from the list type the command !add/remove followed by the role.")

            #Lets start embed roles list below#

            #Colours
            Colours = ['Blue', 'Green', 'Orange', 'Yellow', 'Pink', 'Purple']
            Colours.sort(key=str.lower)
            embed.add_field(name='**__Colour Roles__**', value='\n'.join([":bell: {} **({})**" if role.mentionable in Colours else ":no_bell: {} **({})**".format(role, len([member for member in guild.members if ([r for r in member.roles if r.name == role])])) for role in Colours]))


            await ctx.send(embed=embed)
        else:
            await ctx.send('You can only use this command in {}.'.format(botroom.mention))

更具体地说,错误位于代码中的此行上

 embed.add_field(name='**__Colour Roles__**', value='\n'.join([":bell: {} **({})**" if role.mentionable in Colours else ":no_bell: {} **({})**".format(role, len([member for member in guild.members if ([r for r in member.roles if r.name == role])])) for role in Colours]))

我不确定从这里做什么。 非常感谢。

编辑:回复@Kanasaki Torisowa

Colours = ['Blue', 'Green', 'Orange', 'Yellow', 'Pink', 'Purple']
            Colours.sort(key=str.lower)
            for role in guild.roles:
                if role.mentionable in Colours == True:
                    embed.add_field(name='**__Colour Roles__**', value='\n'.join(["{} **({})**".format(role, len([member for member in guild.members if ([r for r in member.roles if r.name == role])])) for role in Colours]))

1 个答案:

答案 0 :(得分:1)

您可以通过在表情符号的前面添加像这样的\:no_bell:\:bell:

来获得表情符号图标。

@command()
async def mentionable_roles(self, msg):
    channel = self.bot.get_channel(555844758778544160)
    if msg.channel == channel:  # channel is equal to  `555844758778544160`
        emb = discord.Embed(title='Server Roles')  # set the embed title
        for i in msg.guild.roles:  # loop through the roles
            print(i.colour)
            if i.mentionable == True:  # role is mentionable
                # add bell icon since role is mentionable
                emb.add_field(name=i.name, value='')

            if i.mentionable == False:  # role is not mentionable
                # add no bell icon since role is not mentionable
                emb.add_field(name=i.name, value='')

        emb.set_footer(
            text="Tip: to add a role from the list type the command !add/remove followed by the role.")
        await msg.send(embed=emb)
    if msg.channel != channel:
        await msg.send(f"You can only use this command at {channel.name}")