Search code examples
pythondiscorddiscord.py

Discord.py, after 30 minutes have passed since sending a message, I'm unable to edit it


After sending a message using interaction in discord.py, I'm attempting to use buttons to edit it. Editing is possible roughly up to 30 minutes after sending the message, but after that, I encounter the error 'discord.errors.HTTPException: 401 Unauthorized (error code: 50027): Invalid Webhook Token' and cannot edit the message. How can I continue to edit the message after this point?

async def editmsg(msg,con):
    await msg.edit(content=con)

@app_commands.command(name='sendmsg', description='sendmsg')
async def sendmsg(self, interaction: Interaction):
    await interaction.response.send_message(content='message')
    msg = await interaction.original_response()

    # The following code assumes that it is executed when a button is clicked.
    await editmsg(msg,'editmessage')

Solution

  • This issue is caused by the webhook expiring. Webhooks have a lifetime that they are active for and after they expire the webhook becomes invalid. In the case of Discord.py API, the webhooks lifetime is likely less than 30 minutes which is why you are getting this issue.

    Looking at the Discord.py Source Code we can see they use aiohttp.ClientSession and send a request using the session. When this session times out (I couldn't find an exact number in the source code) this request fails and the webhook fetch request gives an error.

    Your issue is caused by the underlying source code of Discord.py and its dependencies. I suggest trying a different approach or creating a ticket on the Discord.py GitHub.