How to get users in role specified in channel

Question:

I’m using discord.py 2.1.0, and I want to get users with role id specified in text channel.

I am trying this approach, but it does not work:

role = discord.utils.get(channel.guild.roles, id=1060070158267326565)
users = role.members
print(users)

output :

[]
Asked By: ahmet güzel

||

Answers:

Seems like you either did not enable the required intent, or there is actually no members with this role.

For intents, check the discord developers portal and enable them. Don’t forget to do it in your code too.

Answered By: Void

I don’t see any problems in your code.

Try to give the bot that role and see, if it returns your bot then:

  • Make sure that guild_members intent is enabled in the dashboard.

enter image description here

Also your code should contain:

intents = discord.Intents.default()
intents.members = True


client = commands.Bot(intents=intents)
Answered By: iRyan23
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.