peppewarrior1
peppewarrior1

Reputation: 55

discord.py:how to resist the result "None" if the member does not have specific roles

I am setting "userinfo" and I created 2 fields where it shows that the highest role of a member among some that I have selected but when this user does not have any of the roles the command (rightly) is no longer executed. How do I get it done despite it not playing a role? I wish it would show other type "None" instead of the role.

@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)

precisely I speak of this part of code:

    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 resistuire "None" come posso fare?

Upvotes: 0

Views: 18

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60974

You can check it and give some default value:

official_mention = official_highest.mention if official_highest else "None"

Upvotes: 1

Related Questions