How can I make a error message when there's no mention on discord.py?

Question:

I’m trying to do a error message when a member puts a text instead of a mention, but I can´t.

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, ext.commands.errors.MissingRequiredArgument):
        await ctx.send(f"{ctx.author.mention}, you should specify a user!")

@bot.command(name="kiss")
async def kiss(ctx, member: discord.Member):
    if ctx.message.channel.is_nsfw() :
        with open ('kiss.json') as kg:
            kissgifs = json.load(kg)
        ksrandomchoice = random.choice(kissgifs)
        embed = discord.Embed(title="Hi")
        embed.set_image(url=ksrandomchoice['kiss'])
        embed.add_field(name=f"{ctx.author.name} kissed {member.display_name}, that's romantic! ❤️", value="")
        await ctx.send(embed=embed)

As you can see, for example if I send a message that says "!-kiss", the bot responds "{ctx.author.mention}, you should specify a user!", that’s the expected result. But if I put "!-kiss randomtext", the boy says nothing. How can I change that?

I tried to use if and else, but that doesn’t work.

Asked By: Masoshi

||

Answers:

Change

if isinstance(error, ext.commands.errors.MissingRequiredArgument):
        await ctx.send(f"{ctx.author.mention}, you should specify a user!")

to

if isinstance(error, ext.commands.errors.MemberNotFound):
        await ctx.send(f"{ctx.author.mention}, you should specify a user!")

Since you don’t want to check if there’s a missing argument you wanna check if its a valid member.

Answered By: Orion447

When you typehint the parameter member with discord.Member, you can use either the name, the ID or the mention to get the discord.Member object.

Because you can’t specify if the user got mentioned via discord.Member, you have to do this via ctx, like this:

@bot.command()
async def kiss(ctx: commands.Context, member: discord.Member = None):
    # Check if member isn't given
    if member is None:
        await ctx.send(f"{ctx.author.mention}, you should specify a user!")
        return

    # Check if the given user isn't mentioned
    if member not in ctx.message.mentions:
        await ctx.send(f"{ctx.author.mention}, you should mention the user!")
        return

    await ctx.send(f"Kiss {member.mention}")

Notice the None as the default value, to prevent a MissingRequiredArgument error if member isn’t given.

To catch the error, when the user can’t be found, you need to use the on_command_error event like that:

@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError):
    if isinstance(error, commands.MemberNotFound):
        await ctx.send(f"{ctx.author.mention}, I can't find this user!")
    else:
        raise error

Note: This event applies globally. Also, you should raise the error, if you don’t handle it, otherwise, you won’t notice other errors, when one is raised.

Answered By: puncher
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.