From 881349b2771d1684dfa2ef34e1421c6a6313e975 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Thu, 19 Aug 2021 14:42:24 +0800 Subject: [PATCH] =?UTF-8?q?autodel=20=E5=9C=A8=E6=8C=87=E5=AE=9A=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=90=8E=E5=88=A0=E9=99=A4=E4=BB=96=E4=BA=BA?= =?UTF-8?q?=E7=9A=84=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autodel.py | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++ dme.py | 2 +- list.json | 10 ++++ vip.py | 4 +- 4 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 autodel.py diff --git a/autodel.py b/autodel.py new file mode 100644 index 0000000..0cbb4a9 --- /dev/null +++ b/autodel.py @@ -0,0 +1,166 @@ +""" Remove Someone's Msg After A Specified Time. """ +# extra requirements: dateparser, redis + +import time +import traceback + +from sys import executable +from asyncio import sleep +from telethon.tl.types import PeerUser +from telethon.tl.custom import Message +from pagermaid import redis, redis_status +from pagermaid.listener import listener +from pagermaid.utils import alias_command + +try: + import dateparser + imported = True +except ImportError: + imported = False + + +# https://stackoverflow.com/questions/1111056/get-time-zone-information-of-the-system-in-python +def local_time_offset(t=None): + """Return offset of local zone from GMT, either at present or at time t.""" + # python2.3 localtime() can't take None + if t is None: + t = time.time() + + if time.localtime(t).tm_isdst and time.daylight: + return -time.altzone + else: + return -time.timezone + + +@listener(incoming=True, ignore_edited=True) +async def remove_others_message(context): + """ Event handler to infinitely remove messages. """ + if not redis_status(): + return + if not context.is_group: + return + chat_id = context.chat_id + if not isinstance(context.from_id, PeerUser): + return + uid = context.from_id.user_id + mid = context.id + data = redis.get(f"autodel.{chat_id}.{uid}") + if data: + delta = float(data.decode()) + await sleep(delta) + # 检查消息是否仍然存在 + chat = await context.get_chat() + msg = await context.client.get_messages(chat, ids=mid) + if msg: + try: + await context.delete() + except Exception as e: + try: + await send_msg(context, await context.get_chat(), str(e)) + except ValueError: + pass + + +@listener(is_plugin=True, outgoing=True, command=alias_command("autodel"), + diagnostics=True, + description=""" +在指定的时间后删除所回复用户发送的消息。只对当前群组有效。 + +i.e. +-autodel 4 seconds +-autodel 1 minutes +-autodel 1 hours + +取消删除消息: + +-autodel cancel +""", + parameters="