简体   繁体   中英

How to make a bot display a user's info if another user pings that user discord.py

I am trying to get my bot to display a users info if a user types 'profile? [name of other user]'.

if message.content == 'profile?' + user.mention:
   x=display_players_smiles(str(user.mention))            
   y=display_players_frowns(str(user.mention))
   embedVar = discord.Embed(title= str(user.mention), description="This is their profile", color=0x0210ff)
   embedVar.add_field(name="Smiles", value=str(x), inline=False)
   embedVar.add_field(name="Frowns", value=str(y), inline=False)
   await message.channel.send(embed=embedVar)

I tried to use user.mention but it gave me an error.

If there's no reason for you to use commands Ill give you an example

@bot.command() # Or `client.command()` - depends how you named your bot instance
async def profile(ctx, user: discord.Member):
    x = display_players_smiles(user.mention)            
    y = display_players_frowns(user.mention)
    embedVar = discord.Embed(title=user.mention, description="This is their profile", color=0x0210ff)
    embedVar.add_field(name="Smiles", value=str(x), inline=False)
    embedVar.add_field(name="Frowns", value=str(y), inline=False)
    await ctx.send(embed=embedVar)

A couple of things worth mentioning:

  • You need intents.members enabled
  • At the end of the on_message event you need to add
await bot.process_commands(message) # Or `client.process..` - depends how you named it
  • Remember to use commands.Bot , not discord.Client , also remember to use only ONE, not both.

Take a look at the commands introduction it will hopefully clear up some things for you.

Here another useful link on how to enable intents & privileged intents, link heer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM