F
F
FreeZe1232020-08-16 00:20:30
Python
FreeZe123, 2020-08-16 00:20:30

How to make sure that when you write !avatar @ (no user, just with @), it doesn't throw an error?

How to make sure that when you write !avatar @ (without user, just with @), it doesn't throw an error ( raise BadArgument('Member "{}" not found'.format(argument))
discord.ext.commands.errors.BadArgument: Member "@" not found
) but something like "User not found."
Here is my code -

@client.command(aliases = ["Аватар","Avatar","аватар"])#avatar
async def avatar(ctx, member : discord.Member = None):
    if ctx.channel.id == 698879971942203524:
        user = ctx.message.author if (member == None) else member
        embed = discord.Embed(title=f'Avatar {user.name}', color= 0x9006E6)
        embed.set_image(url=user.avatar_url)
        embed.set_footer(text="Команда вызвана: {}".format(ctx.author.name), icon_url=ctx.author.avatar_url)
        await ctx.send(embed=embed)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-08-16
@FreeZe123

Catching an error in an event on_error_command(context, error): https://discordpy.readthedocs.io/en/v1.4.1/ext/com...
Or in an inherited class:

from discord.ext import commands
from contextlib import suppress

class MyBot(commands.Bot):
    async def on_command_error(self, ctx, error):
        if isinstance(error, commands.BadArgument):
            await ctx.send(f"Неверный аргумент: {error.message}")
        else:
            await super().on_command_error(ctx, error)  # вызывает изначальное поведение on_error_message

bot = MyBot(command_prefix="!")

Or just in the "listener" function:
from discord.ext import commands
from contextlib import suppress

bot = commands.Bot(command_prefix="!")

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.BadArgument):
        await ctx.send(f"Неверный аргумент: {error.message}")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question