Search code examples
pythondiscord

discord.py get guild members


I'm creating a bot in discord.py and I am creating a command to kick all non-roled server members.

The problem however is, I see the bot as the only member. I tried researching the docs, but I couldn't find a clear example or answer.

Here's my code:

@bot.command()
async def kick_non_role(ctx, *, reason=None):
  print(ctx.guild.members)
  for member in ctx.guild.members:
      print(member)
      if len(member._roles) == 1:
          await member.kick(reason='No role assigned')
          print(f'Non-roled members have been kicked from the server.')
      print(f'{member} has roles: {len(member._roles)}')

Here's the output:

[<Member id=904484713937379390 name='Bouncer_Bot' discriminator='5970' bot=True nick=None guild=<Guild id=867621531320057887 name='SixtyNiners' shard_id=None chunked=False member_count=10>>]
Bouncer_Bot#5970
Bouncer_Bot#5970 has roles: 3

Solution

  • Thanks to @Nyghi for posting the thread. My issue was hierarchy of roles in my server, set the bot role to the top and my code went to work.

    discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions error for setting up roles