Python Discord bot send text upon exception

Viewed 398

I have a function in my Discord bot that acts as a magic 8 ball where you ask it a question and gives a random reply. An exception occurs when the function is called but no question is asked, and I would like to get an error message of some kind print to the channel it was called in notifying the user that this error as occurred. Here's what I attempted:

@client.command(aliases=['8ball', 'eightball'])
async def _8ball(ctx, *,question):
    responses = ['yes', 'no', 'maybe so']
    response = random.choice(responses)
    try:
        await ctx.send(f'Question: {question}\n\
        Answer: {response}')
    except MissingRequiredArgument:
        await ctx.send("Please ask a question")
        pass

But nothing prints out except in the terminal. Any suggestions?

1 Answers
@client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument) and "replace me" in str(error):
        await ctx.send("You're missing a required argument idiot.¯\_(ツ)_/¯")

With discord.py you can't really use a try and except. You'll need to check with an on_command_error event. Replace the "replace me" with whatever comes out after your MissingRequiredArgument error. For example MissingRequiredArgument: arg is a required argument that is missing. Then you'll replace "replace me" with "arg is a required argument that is missing"

Btw, next time when you receive errors in your code, please add them to your question. It helps us understand what went wrong.

Related