Tanuj KS
Tanuj KS

Reputation: 165

discord.py raise different errors for different check failures

I have a discord.py bot with the command test. I want to run two checks, first check if the person running it is the bot owner and check if the command is run in a dm. If the bot is run in a dm I want it to send a message but if its not the owner I want it to run a different message. how would I do this?

@bot.command()
@commands.guild_only()
@commands.check(is_owner)
async def test(ctx):
    await ctx.send("Good job you passed the checks!")
@test.error
async def test_error(ctx, error):
    if failed guild_only():
        await ctx.send("Hey only use this in a guild")
    if failed is_owner():
        await ctx.send("You're not the owner!!")

Upvotes: 0

Views: 583

Answers (1)

kr8gz
kr8gz

Reputation: 378

Use isinstance().

isinstance checks whether if an object has the specified type. For example, isinstance(3, int) checks if 3 is an integer (which it is and therefore returns True). In your case you could check if the error is commands.NoPrivateMessage, which is raised by guild_only, or if the error is commands.NotOwner, which is raised by is_owner. The correct code would be:

@test.error
async def test_error(ctx, error):
    if isinstance(error, commands.NoPrivateChannel):
        await ctx.send("Hey only use this in a guild")
    if isinstance(error, commands.NotOwner):
        await ctx.send("You're not the owner!!")

Upvotes: 1

Related Questions