Midoss
Midoss

Reputation: 35

I want my bot to tell you that you must mention someone, when you dont

I have bunch of fun commands like "hug, marry etc."

They are working with

.hug user

but ı want my python bot tell "You need to mention someone" if you dont mention a user. However, whenever I test the bot, nothing happens and users cant know that they need to mention a user

My code:

async def hug(ctx,user:discord.Member):
    if not discord.Member:
        await ctx.send("you need to mention someone")
        return

    embed = discord.Embed(title=f"{ctx.author} hugs {user.name}", description="nice...")
    embed.set_image(url="url")

    await ctx.send(embed=embed)``
   

Upvotes: 2

Views: 138

Answers (2)

Billy
Billy

Reputation: 1207

The way you are checking, is wrong, you defined an attribute named user and gave it a type discord.Member, so you need to check whether user is passed.

async def hug(ctx, user:discord.Member):
    if not user:
        await ctx.send("you must mention a user!")
        return

    embed = discord.Embed(title=f"{ctx.author} hugs {user.name}", description="nice...")
    embed.set_image(url="url")

    await ctx.send(embed=embed)

The problem, if you are checking len(ctx.message.mentions) != 1, is that when you use: .hug xyz @billy it is also gonna pass the if condition, but not gonna work, instead it will throw an error. because a BadArgument - xyz is passed second.

Upvotes: 0

Raz Kissos
Raz Kissos

Reputation: 295

The solution is very simple, the message object has a mentions attribute, you can check the length of the message's mentions and if they are not equal to 1 then there should be an error.

async def hug(ctx, user:discord.Member):
    if len(ctx.message.mentions) != 1:
        await ctx.send("you must mention a user!")
        return

    embed = discord.Embed(title=f"{ctx.author} hugs {user.name}", description="nice...")
    embed.set_image(url="url")

    await ctx.send(embed=embed)``

The Discord API documentation about the message.mentions attribute: https://discordpy.readthedocs.io/en/latest/api.html?highlight=message%20mentions#discord.Message.mentions

Upvotes: 2

Related Questions