#send error message

1 messages · Page 1 of 1 (latest)

regal cove
#

I am trying to send an error message with the error listener, though im getting
AttributeError: 'NoneType' object has no attribute 'channel_id'

async def on_error(error:Error):
    x = error.ctx.channel_id #Error is here
    with open('errors.txt', 'a') as f:
        f.write(f"{error.source}\n{error.error}")
    print(f"error.error: {error.error}\nerror.source: {error.source}\n")
    
    embed = interactions.Embed(
        title="Error",
        description="Error",
        color=BrandColors.RED
    )
    await (await bot.fetch_channel(x)).send(embed=embed)

I dont know why im getting a NoneType since I put the Error in the decorator

loud novaBOT
#

Hey! Once your issue is solved, press the button below to close this thread!

civic sigil
#

ctx is optional, it can be None

#

I guess that means that it errored somewhere without an interaction?

valid geyser
#

assuming this is for a command error (such as from running slash commands), you can listen to CommandError instead of Error. this also tripped me up while working on my bot

doing it this way you can simply use ctx.send() without the extra step of fetching from the channel id

#

some caveats for above:
- there's no error.source, instead use error.ctx.invoke_target for the command name
- error.ctx will be an instance of BaseContext which itself has no send(). you can make sure by wrapping the error.ctx.send() in if isinstance(error.ctx, SendMixin): since SendMixin (which includes InteractionContext for slash commands) will have send().

celest otter
#

(as for why SendMixin in instead of InteractionContext - its mostly just good practice, but it also means you cover prefixed commands if you ever want to use them too)

#

but yeah, Error is used for non-event errors, ie errors in events

regal cove
#

Im seeing that error.ctx is BaseContext, so how am I supposed to use ctx.send() ? I cant use invoke_target either since its BaseContext:

async def on_error(error: CommandError):
    x = error.ctx.send(content='???')
    y = error.ctx.invoke_target

Unresolved attribute reference 'send' for class 'BaseContext' (same for invoke_target)

celest otter
#

...scroll up

valid geyser
#

wrap your ctx.send() in if isinstance(error.ctx, SendMixin):

#

as for invoke_target, since that's part of BaseInteractionContext, you can use that in place of SendMixin.

(advanced) you can also have additional logic to alter y ("command name") according to the type of context

regal cove
#

now its saying its a CommandError object

from interactions.client.mixins.send import SendMixin
#Other imports

@listen(CommandError, disable_default_listeners=True)
async def on_error(error: CommandError):
    print(type(error.ctx))
    if isinstance(error.ctx, SendMixin):
        await error.send(content='???') #Error here
    else:
        print("what")

AttributeError: 'CommandError' object has no attribute 'send'

#

Also for invoke_target would I put BaseInteractionContext as a function parameter

valid geyser
#

error.ctx.send not error.send

#

also the actual exception is in error.error

#

if you're only handling slash commands, you can use if isinstance(error.ctx, BaseInteractionContext): and pull from error.ctx.invoke_target in it. not ideal but it may work in your case

regal cove
#

Ok I got it working now, thank you

regal cove
#

I got another issue but its unrealted to this one, so im gonna make a new thread, thanks everyone for the help