Можно использовать такое решение:
from threading import Thread
from collections import namedtuple
import time
import typing
import random
Message = namedtuple('Message', ['delete_time', 'message_id'])
messages_to_delete: typing.List[Message] = []
def delete_message(message: Message):
print(f'Сообщение {message.message_id=} удалено')
messages_to_delete.remove(message)
def messages_deleter():
while True:
for message in messages_to_delete:
if message.delete_time - time.time() <= 0:
delete_message(message)
time.sleep(1)
if __name__ == '__main__':
thread = Thread(target=messages_deleter)
thread.start()
while True:
delete_time = time.time() + random.randint(1, 5)
messages_to_delete.append(
Message(delete_time=delete_time, message_id=random.randint(1, 1000))
)
time.sleep(1)