Search code examples
pythondiscord.pybotsmessagecustom-error-handling

how to make your bot send what message the user send when an error happens


i was trying to code what to do when an error happens but i always get this when an error actually happen

error found by: gHoSt FaCe#xxxx, in: admins-chat,  the message was: <Message id=1023531531035230208 channel=<TextChannel id=1019911370512289802 name='admins-chat' position=22 nsfw=False news=False category_id=1019911233077526578> type=<MessageType.default: 0> author=<Member id=593874348524175405 name='gHoSt FaCe' discriminator='2856' bot=False nick=None guild=<Guild id=744535554036858901 name='MJSH21' shard_id=0 chunked=True member_count=53>> flags=<MessageFlags value=0>>, the error: Member "test" not found.

code:

@client.event
async def on_command_error(ctx, error):
    channel = client.get_channel(xxxxxxxx)
    await channel.send(f'error found by: {str(ctx.author)}, in: {str(ctx.channel)},  the message was: {str(ctx.message)}, the error: {str(error)}')
    await ctx.send(f"An error occured: {str(error)} ")

every time it gets to the message it just sends a lot of weird stuff it still works but how can i remove the weird stuff and only send the message instead of all of that weird stuff

imports:

from discord.ext import commands
import discord.member
from dotenv import load_dotenv
import discord
import os
import time
from discord.utils import get
from discord.ext.commands import has_permissions, guild_only, Bot

Solution

  • You put ctx.message alone, which is what returns all the weird stuff
    in order to remove the weird stuff you just add .content making it ctx.message.content

    Also you don't need to convert anything to a string it will work without it

    The code will look like this

    @bot.event
    async def on_command_error(ctx:commands.Context, error):
        channel = bot.get_channel(xxxxxxxx)
        await channel.send(f'error found by: {ctx.author}, in: {ctx.channel},  the message was: {ctx.message.content}, the error: {error}')
        await ctx.send(f"An error occured: {str(error)} ")
    

    It's better if you add the :commands.Context to ctx in order to understand what else you can do