Search code examples
pythondiscorddiscord.py

Discordpy Comand userinfo isn't working, it says that the information is not in list


I have this command to show userinfo in my discord bot

@client.command()
async def userinfo(ctx, *, user: discord.Member = None):
    if isinstance(ctx.channel, discord.DMChannel):
       return
    if user is None:
        user = ctx.author      
    date_format = "%a, %d %b %Y %I:%M %p"
    embed = discord.Embed(color=0xdfa3ff, description=user.mention)
    embed.set_author(name=str(user), icon_url=user.avatar_url)
    embed.set_thumbnail(url=user.avatar_url)
    embed.add_field(name="Unido", value=user.joined_at.strftime(date_format))
    members = sorted(ctx.guild.members, key=lambda m: m.joined_at)
    embed.add_field(name="Posicion de Servidor", value=str(members.index(user)+1))
    embed.add_field(name="Registrado", value=user.created_at.strftime(date_format))
    if len(user.roles) > 1:
        role_string = ' '.join([r.mention for r in user.roles][1:])
        embed.add_field(name="Roles [{}]".format(len(user.roles)-1), value=role_string, inline=False)
    perm_string = ', '.join([str(p[0]).replace("_", " ").title() for p in user.guild_permissions if p[1]])
    embed.add_field(name="Permisos de Administracion", value=perm_string, inline=False)
    embed.set_footer(text='ID: ' + str(user.id))
    return await ctx.send(embed=embed)

But it shows an error:

File "main.py", line 280, in userinfo embed.add_field(name="Posicion de Servidor", value=str(members.index(user)+1)) ValueError: <Member id=568157479020527636 name='ElmerKao' discriminator='0058' bot=False nick=None guild=> is not in list

Anyone can explain what is happening?


Solution

  • I manage to repair it, the userinfo code was perfect, but at the top of the code I had this writen:

    #NECESAROS
    intents = discord.Intents.default()
    intents.members = True
    startup = time.time()
    
    #PREFIJO COMANDO
    client = commands.Bot(command_prefix = '!', intents=discord.Intents.default(), help_command=None)
    

    But it should be like this in order to make it work poperly

    #NECESAROS
    intents = discord.Intents.default()
    intents.members = True
    startup = time.time()
    
    #PREFIJO COMANDO
    client = commands.Bot(command_prefix = '!', intents=intents, help_command=None)
    

    This fixed my problem, it happend because I was trying to enable slash commands「With this part intents=discord.Intents.default()」and that was needed it seems that making that broke my userinfo command

    This is how it looks like This is how it looks like