Search code examples
pythondiscorddiscord.pypython-requests-html

Instantly delete discord messages using python discord.py


Hello I'm trying to make this code unsend my messages without having the permission or role to delete/manage messages in a server

I have tried using "delete_after" argument but most of the times messages doesn't get deleted and I'm not sure why

This is my code:

@client.command(pass_context=True)
async def hello(ctx):
    while True:
        time.sleep(int(timer))
        await ctx.send("HELLO", delete_after=1)

Solution

  • The deletions can fail sometimes if you hit ratelimits, the message falls out of cache, etc.

    See the docs. If the deletion fails, it will be silently ignored.

    If you want to be completely sure they are deleted, you need to save the messages and delete them manually. If this fails, then you will get an error rather than nothing.

        while True:
            await asyncio.sleep(int(timer-DELETE_AFTER_DURATION))
            message = await ctx.send("HELLO")
            await asyncio.sleep(DELETE_AFTER_DURATION)
            await message.delete()
    

    Sidenote: use asyncio.sleep, which won't block the rest of your code, rather than time.sleep, anywhere in a coroutine.