I’m trying to implement a command in my Discord bot that lists all the members in my server using an embed. However, when I execute the command, it only returns object references instead of the member usernames.
Here’s the code I’m using:
@bot.command()
async def list_members(ctx):
embed = discord.Embed(title="Server Members", color=0x00ff00)
member_names = [member.name for member in ctx.guild.members]
embed.add_field(name="Members:", value=', '.join(member_names), inline=False)
await ctx.send(embed=embed)
I’m quite new to coding and would really appreciate any guidance on this issue. Thanks in advance!
This is probably a Discord member caching issue. Your code looks fine, but Discord bots don’t cache all members by default, especially on bigger servers. You need to enable privileged gateway intents in two places: your bot settings on the Discord Developer Portal AND in your code. Add intents = discord.Intents.default()
then intents.members = True
before creating your bot instance. If you’re getting object references instead of usernames, double-check that you’re actually working with Member objects. I had a similar problem where my bot returned wrong cached data - try adding print(type(member))
in your loop to see what data type you’re actually handling.
Your code structure looks good, but watch out for a few things. First, make sure you’ve got member intents enabled in the developer portal AND in your code with discord.Intents.members()
. Without it, you’ll get an incomplete members list. Second, Discord embed fields max out at 1024 characters - if your server’s big, you’ll hit that limit fast. You’ll need to split members across multiple fields or add pagination. Also heads up that some usernames have special characters that can mess with formatting. I ran into all these issues with my first member list command and ended up adding pagination for bigger servers.
hey, ur code seems fine, but try using member.display_name
instead of member.name
. also, make sure ur bot has perms to see member list. check if the right intents are enabled too, otherwise it won’t work right.