Search code examples
discorddiscord.pyindex-error

Is it possible to print out an IndexError from a discord bot?


I want to print out and IndexError("List index out of range") from my Discord bot into a channel if something is incorrectly executed. However my method, which I also used for AttributeErrors, does not work. Where is the mistake I made?

try:
    message = await channel.fetch_message(id_)
except IndexError:
    return await ctx.send('The ID that was entered was incorrect, make sure you have entered the correct giveaway message ID.')

Solution

  • Messageable.fetch_message never raises IndexError, it raises the following:

    Answering your question - you can simply except discord.NotFound error

    try:
        message = await channel.fetch_message(message_id)
    except discord.NotFound:
        await ctx.send("The ID that was entered is incorrect, make sure you have entered the correct giveaway message ID.")