Why I can't name a user without mention it on Discord.py?

Question:

I’m trying to name a user that are previously mentioned, but I can’t. This is the code:

@bot.command(name="kiss")
async def kiss(ctx, user):
    if ctx.message.channel.is_nsfw():
        with open ('kiss.json') as kg:
            kissgifs = json.load(kg)
        ksrandomchoice = random.choice(kissgifs)
        embed = discord.Embed()
        embed.set_image(url=ksrandomchoice['kiss'])
        embed.add_field(name=f"{ctx.author.name} kissed {user.mention}", value="")
        await ctx.send(embed=embed)
    else:
        await ctx.send("I'm not")

I tried using discord.User.display_name, but shows this: "<property object at 0x7f6a6106cef0>"

Asked By: Masoshi

||

Answers:

Let’s take a look at your code on line 2 :

async def kiss(ctx, user):

Here, you declare a parameter user. I assume that you want users to mention other users, such as .kiss @Someone. But in order to do this, you have to hint Discord on the fact that the variable "user" actually represents an user. You can do this by precising the type of variable. It would look like this :

async def kiss(ctx, user: discord.User):

Once done, you can use user.name in your code to name (or user.mention to mention) the user. Hope this helps !

Answered By: Deden
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.