Finlay Metcalfe
Finlay Metcalfe

Reputation: 143

Cannot get discord to show error in chat

I'm trying to make my bot tell a user if there is a error by spitting out You do not have the permissions {} But when I try to use this code:

@client.command(pass_context = True)
async def ban(ctx, member : discord.Member, *, content: str):
    if ctx.message.author == client.user:
        return
    if ctx.message.author.server_permissions.administrator:
        msg = (str(member) + "has been banned for" + str(content)).format(ctx.message)
        await client.send_message(member, content)
        await client.ban(member)
        await client.send_message(ctx.message.channel, msg)
@ban.error
async def ban_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "Sorry but you do not have the permissions {}".format(ctx.message.author.mention)  
        await client.send_message(ctx.message.channel, msg)

The discord bot dms the user and there is no errors in the python console and if I remove the @ban.error piece I get a too low of a permission error.

Upvotes: 1

Views: 685

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 61052

CheckFailure only gets raised if a check fails. There are no checks in your code, so this never happens. You can convert your if statement to a check very easily using commands.has_permission:

@client.command(pass_context = True)
@has_permissions(administrator=True)
async def ban(ctx, member : discord.Member, *, content: str):
    msg = "{} has been banned for {}".format(ctx.message.author.mention, content)
    await client.send_message(member, content)
    await client.ban(member)
    await client.send_message(ctx.message.channel, msg)

It may be the case that the error handler is suppressing valuable error information. We can just call the bots built in error handling for any other errors that are not CheckFailures

from discord.ext.commands import CheckFailure
from discord import Forbidden


@ban.error
async def ban_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "Sorry but you do not have the permissions {}".format(ctx.message.author.mention)  
        await client.send_message(ctx.message.channel, msg)
    elif isinstance(error, Forbidden):
        await client.send_message(ctx.message.channel, "I do not have the correct permissions")
    else:
        print(error)
        await client.on_command_error(error, ctx)

Upvotes: 1

Related Questions