discord.py:如果成员没有特定角色,如何抵制结果“无”

时间:2020-07-17 15:17:24

标签: discord.py

我正在设置“ userinfo”,并创建了2个字段,其中显示了我所选择的某个成员中成员的最高角色,但是当该用户不具有任何角色时,将不再执行该命令(正确) 。 尽管没有发挥作用,我如何完成它?我希望它会显示其他类型的“无”而不是角色。

@client.command(aliases=["UserInfo","User_info","Userinfo","USERINFO","ui","Ui","UI"])
@commands.has_any_role('Moderatori', 'Triumvirato', 'Co-Triumvirato', 'Senatori', '690956686147453048')
async def userinfo(ctx, user: discord.Member):
    guild = ctx.guild
    #ruoli-principali
    triumvirato = get(guild.roles, id=int("690951634183782461"))
    co_triumvirato = get(guild.roles, id=int("690954867346243624"))
    presidente = get(guild.roles, id=int("690956686147453048"))
    senatore = get(guild.roles, id=int("690960692051705896"))
    moderatore = get(guild.roles, id=int("700353561392971877"))
    membro = get(guild.roles, id=int("690963300707729408"))
    accademico = get(guild.roles, id=int("690964416644251750"))
    onorario = get(guild.roles, id=int("690965300769980476"))
    gamer = get(guild.roles, id=int("724978599266091028"))
    clandestino = get(guild.roles, id=int("690972809219801088"))
    #ruoli-attività
    storico = get(guild.roles, id=int("690967861404893234"))
    boosters = get(guild.roles, id=int("726498072581898250"))
    veterano = get(guild.roles, id=int("690962271962267758"))
    rilevante = get(guild.roles, id=int("690962450211799070"))
    attivo = get(guild.roles, id=int("690962456897650755"))
    presente = get(guild.roles, id=int("690962769511710811"))
    ambientato = get(guild.roles, id=int("690962461255270440"))
    recluta = get(guild.roles, id=int("690962671847342171"))
    #settaggio ruoli
    official_roles = clandestino, onorario, gamer, accademico, membro, moderatore, senatore, presidente, co_triumvirato, triumvirato
    official_highest = discord.utils.find(lambda role: role in official_roles, reversed(user.roles))
    activity_roles = recluta, ambientato, presente, attivo, rilevante, veterano, boosters, storico
    activity_highest = discord.utils.find(lambda role: role in activity_roles, reversed(user.roles))
    #esecuzione comando
    embed = discord.Embed(
        title=f'Informazioni su {user}',
        timestamp=ctx.message.created_at,
        color=user.color
    )
    embed.set_footer(
        text=f'Richiesto da: {ctx.author}',
        icon_url=ctx.author.avatar_url
    )
    embed.set_thumbnail(url=user.avatar_url)
    embed.add_field(
        name='ID utente:',
        value=user.id,
        inline=False
    )
    embed.add_field(
        name='Nome utente:',
        value=user.display_name,
        inline=False
    )
    embed.add_field(
        name='Account creato il:',
        value=user.created_at.strftime("%a, %d %B %Y, %I:%M %p CET"),
        inline=False
    )
    embed.add_field(
        name="L'utente è entrato nel server il:",
        value=user.joined_at.strftime("%a, %d %B %Y, %I:%M %p CET"),
        inline=False
    )
    embed.add_field(
        name="Ruolo ufficiale:",
        value=f'{official_highest.mention}',
        inline=False
    )
    embed.add_field(
        name="Ruolo attività:",
        value=f'{activity_highest.mention}',
        inline=False
    )
    embed.add_field(
        name="E' un bot:",
        value=user.bot,
        inline=False
    )
    await ctx.send(embed=embed)

确切地说,我说的是这部分代码:

    official_roles = clandestino, onorario, gamer, accademico, membro, moderatore, senatore, presidente, co_triumvirato, triumvirato
    official_highest = discord.utils.find(lambda role: role in official_roles, reversed(user.roles))
    activity_roles = recluta, ambientato, presente, attivo, rilevante, veterano, boosters, storico
    activity_highest = discord.utils.find(lambda role: role in activity_roles, reversed(user.roles))

se l'utente selezionato non ha nessuno dei ruoli presenti su activity_highest deve resuruire“ None” posso far?

1 个答案:

答案 0 :(得分:1)

您可以检查它并提供一些默认值:

dig k8s118.devops.link

; <<>> DiG 9.11.3-1ubuntu1.12-Ubuntu <<>> k8s118.devops.link
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 595
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;k8s118.devops.link.            IN      A

;; Query time: 495 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Jul 18 00:36:07 +04 2020
;; MSG SIZE  rcvd: 47