0

Currently running the below code to find if the author of a message has a role with a given name:

    author = await context.guild.fetch_member(context.author_id)
    role = discord.utils.find(lambda r: r.name == 'premium', context.guild.roles)
    if role not in author.roles:
        some code

It works when there is a single role to the role var.

What I want is to see if the message author doesn't have roles that are given in a list. Like the below example - which doesn't work (it doesn't execute the if statement even when author has the "verified" role):

    author = await context.guild.fetch_member(context.author_id)
    role = discord.utils.find(lambda r: r.name in ['premium','verified'], context.guild.roles)
    if role not in author.roles:
        some code

How can I correctly adjust or replace the lambda function to search if any of the given role names are present in author.roles?

RiveN
  • 2,595
  • 11
  • 13
  • 26
Gino
  • 15
  • 9

1 Answers1

0

I prepared code for two scenarios. In the first example, you can use issubset() which will check if all roles from the list are in author.roles and second if any role is present by using any().


1. When the user doesn't have all roles from the list:

author = await context.guild.fetch_member(context.author_id)
role_list = ["premium", "verified"]
roles = [discord.utils.get(guild.roles, name=x) for x in role_list] #by looping we can get multiple roles and assign them to list

if not set(roles).issubset(author.roles): #this will execute when user doesn't have all roles from list
    # some code

2. When the user doesn't have any role from the list:

author = await context.guild.fetch_member(context.author_id)
role_list = ["premium", "verified"]
roles = [discord.utils.get(guild.roles, name=x) for x in role_list]

if not any(i in author.roles for i in roles): #this will execute only when user doesn't have at least one role from the list
    # some code
RiveN
  • 2,595
  • 11
  • 13
  • 26