0

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?

rgegvre
  • 3
  • 2

1 Answers1

0

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
svex99
  • 518
  • 4
  • 10
  • thanks, but honestly i'm not an expert in python, I tried to add the line to the code but I getting "IndentationError:" here is code that i'm using https://pastebin.com/BTJdZu4h – rgegvre Jan 25 '22 at 03:08
  • You get `IndentationError` if are mixed tabs and spaces when you indent your code. Probably because you copied the code from this website. Also, you can check this related question https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it – svex99 Jan 25 '22 at 03:16
  • I was using the code from month ago was working fine, but if I add `if await client.get_messages(event.sender_id, ids=[event.id]):` to the code I get `IndentationError` – rgegvre Jan 25 '22 at 03:35
  • I tested [this code](https://pastebin.com/yeGV7NQB) and it doesn't raise `IndentationError`. Please share your full code and the exception it raises if you're still stuck. – svex99 Jan 25 '22 at 04:01
  • [this one](https://pastebin.com/i51xQsSK) will gets you `File "C:\Users\Vtt\Desktop\forward\forward.py", line 49 for output_channel in output_channel_entities: ^ IndentationError: expected an indented block` and if you removed line 48 will works fine – rgegvre Jan 25 '22 at 04:16
  • I fixed your code [here](https://pastebin.com/BBBbBuCJ). The problem is that you have to indent the content inside the `if` statement. In this case the `for` loop at line 49 cannot have the same indentation that has the `if`. – svex99 Jan 25 '22 at 04:54
  • thanks, code working, but the peoblem hasn't fixed yet, you see if I changes it to `client.send_message` it will work as copy message not forward and it's still send deleted message from cahnnel A to B even if it's deleted before timing of delay – rgegvre Jan 25 '22 at 05:27
  • just to clear of what code do, it's listen to channel "A" and send "copy"/forward messages to channel "B". I use delay time because channel "A" sometimes updates and remove some message in the time of delay, so when delay time is done it will transfer only message that is in channel A to B (without deleted messages) it's works fine with forward as it should, but using send "copy" it's sending even deleted message (after delay time is done). what I want is that not sending deleted message if it's deleted before delay time is done. – rgegvre Jan 25 '22 at 05:37
  • Ok, as I answered you need to check if a message is deleted or not, using the function `get_messages`. If the message is not deleted use `send_message` to send to other channels, if deleted do nothing. Make sure you have your conditionals well placed in your code. I'm not clear if you get the desired result or not with `forward_messages` and why are you trying with both. – svex99 Jan 25 '22 at 06:02
  • using `forward_messages` didn't change anything after add the code you suggested to add since is working fine before. what I want is to fix it if I use `send_message` and thanks after all – rgegvre Jan 25 '22 at 06:23
  • Sorry for the inconvenience. The problem was that the `get_messages` function if the message was deleted still returns it in the list as `None`, so always the length of the list was > 1 and the `if` evaluated to `True`. I edited the code in the post, instead of check for the length of the list, now checks if the message that is at index 0 is `None` or not. Please make sure to mark the answer as correct if you get over the issue now, thank you. – svex99 Jan 25 '22 at 18:21
  • This works fine with both `send.message` and `forward.messages`. Thanks for your effort and time. – rgegvre Jan 25 '22 at 19:13