Skip to content Skip to sidebar Skip to footer

Retrieve List Of Members In Channel Discord.py Rewrite

Summary I'm really new to discord.py and am trying to figure out how to retrieve a list of people in a discord server channel. I'm working on a command that would randomly separate

Solution 1:

You can do this by using discord.utils.get. Here is an example. you can also use await bot.fetch_guild(guild_id) to get the guild object instead of ctx.guild

Note: bot.fetch_guild require you to use discord.ext.commands

@bot.command()asyncdefvoice(ctx):
    VC = discord.utils.get(ctx.guild.channels, id=705524214270132368)
    print(VC.members)  # each user is a member objectfor user in VC.members:
        # Code hereprint(user.name)

Solution 2:

You can do this by VoiceChannel.members, but some things should be changed from Abdulaziz's answer.

1.Use Bot.get_channel()

  • Use bot.get_channel if you have channel's ID or Guild.get_channel if you know in which Guild the channel is and have its ID, utils.get should be called only when you don't have ID.

  • get_ methods are O(1) while utils.get is internally a for loop, therefore O(n).

  • If you're not familiar with big O notation, O(1) means operation time is the same with any number of items while O(n) is linear, the more items there are, the longer it takes.

  • Read more about O(n) vs O(1)here.

2. Use Bot.get_guild()

  • fetch_guild is an API call and can get your bot rate-limited soon while get_guild is not an API call.

  • Using the Guild object returned by fetch_guild will not give you the guild's channels.

  • Read more about it in the documentation here.

Here is the revised code:

@bot.command()asyncdefteam(ctx):
    vc = ctx.guild.get_channel(742871631751282829)
    await ctx.send("\n".join([str(i) for i in vc.members]))

Post a Comment for "Retrieve List Of Members In Channel Discord.py Rewrite"