gpt4 book ai didi

python - 试图展示值得提及的角色的问题

转载 作者:行者123 更新时间:2023-12-01 08:09:42 24 4
gpt4 key购买 nike

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

我想要完成但未能做到的事情是能够在可提及的角色前面设置 add :bell: 而对于不可提及的角色设置 :nobell: 。

我收到错误 AttributeError: 'str' 对象没有属性 '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]))

最佳答案

您可以通过在表情符号前面添加正斜杠来获取表情符号图标,如下所示 \: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}")

关于python - 试图展示值得提及的角色的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55329806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com