This bug related to code that posted by @Lonami here #3250
the code
import asyncio, functools
def delayed(seconds):
def decorator(func):
@functools.wraps(func)
async def wrapped(*args, **kwargs):
await asyncio.sleep(seconds)
return await func(*args, **kwargs)
return wrapped
return decorator
...
@client.on(events...)
@delayed(5)
async def handler(...): ...
the code works fine to delay send/forward message to channels. but the problem is if i'm using send.message the code copy the message and send it even if it's deleted in source channel
so, is there a way to not sending messages that have been deleted in source channel before sending it to destination channel?
I want to point out that you are receiving an update (the event) but this event is arriving delayed to the handler (5 seconds in this case). So when the event is used in your handler you should consider that is an "old" event and maybe some of his data is not valid at this time.
The "old" event that gets the handler have the original text of the message and other data, such the id of the message in the channel, but note that the original message's text could be edited, the message deleted or even deleted the channel, but your event don't reflects this updates that may be happened in the last 5 seconds.
I think you can instead of send the event text directly, you can check that the message that issued the event still is in the original channel (this requires an API call) before send the message to a new channel.
async def handler(event):
# get_messages returns a telethon.helpers.TotalList with the messages
# if a meesage in this list is None, means it was deleted
if (await client.get_messages(event.sender_id, ids=[event.id]))[0] is not None:
# do your stuff
else:
# do your other stuff