From f233dbdd8bb273d93f2fa306e05f7159a5492774 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Thu, 15 Jul 2021 18:12:14 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20forcesubscribe=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=9C=AA=E5=85=B3=E6=B3=A8=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E5=85=AC=E5=BC=80=E9=A2=91=E9=81=93=E7=9A=84=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=9A=84=E5=8F=91=E8=A8=80=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- forcesubscribe.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++ list.json | 10 +++++ 2 files changed, 106 insertions(+) create mode 100644 forcesubscribe.py diff --git a/forcesubscribe.py b/forcesubscribe.py new file mode 100644 index 0000000..fc00bd5 --- /dev/null +++ b/forcesubscribe.py @@ -0,0 +1,96 @@ +from pagermaid import bot, redis, redis_status +from pagermaid.utils import lang, alias_command +from pagermaid.listener import listener +from telethon.events.chataction import ChatAction +from telethon.tl.custom.message import Message +from telethon.tl.functions.channels import GetParticipantRequest +from telethon.errors.rpcerrorlist import UserNotParticipantError, ChatAdminRequiredError +from telethon import events + + +@bot.on(events.ChatAction()) +async def force_subscribe_join(event: ChatAction.Event): + if not redis_status(): + return + if not (event.user_joined or event.user_added): + return + join_chat = redis.get(f"sub.chat_id.{event.chat_id}") + if not join_chat: + return + else: + join_chat = join_chat.decode() + user = await event.get_user() + if user.bot: + return + if redis.get(f"sub.chat_id.{event.chat_id}.{user.id}"): + return + try: + await bot(GetParticipantRequest(join_chat, user.id)) + redis.set(f'sub.chat_id.{event.chat_id}.{user.id}', 'true') + redis.expire(f'sub.chat_id.{event.chat_id}.{user.id}', 3600) + except UserNotParticipantError: + await event.reply(f'[{user.first_name}](tg://user?id={user.id}) 您需要先加入频道 @{join_chat} 才能发言。') + except ChatAdminRequiredError: + redis.delete(f"sub.chat_id.{event.chat_id}") + + +@listener(is_plugin=True, incoming=True, ignore_edited=True) +async def force_subscribe_msg(context: Message): + if not redis_status(): + return + join_chat = redis.get(f"sub.chat_id.{context.chat_id}") + if not join_chat: + return + if redis.get(f"sub.chat_id.{context.chat_id}.{context.sender_id}"): + return + else: + join_chat = join_chat.decode() + if context.sender.bot: + return + try: + await bot(GetParticipantRequest(join_chat, context.sender_id)) + redis.set(f'sub.chat_id.{context.chat_id}.{context.sender_id}', 'true') + redis.expire(f'sub.chat_id.{context.chat_id}.{context.sender_id}', 3600) + except UserNotParticipantError: + try: + await context.delete() + await bot.send_message(context.chat_id, + f'[{context.sender.first_name}](tg://user?id={context.sender_id}) ' + f'您需要先加入频道 @{join_chat} 才能发言。') + except ChatAdminRequiredError: + redis.delete(f"sub.chat_id.{context.chat_id}") + except ChatAdminRequiredError: + redis.delete(f"sub.chat_id.{context.chat_id}") + + +@listener(is_plugin=False, outgoing=True, command=alias_command('forcesub'), + description='自动删除未关注指定公开频道的用户的发言。', + parameters="") +async def force_sub(context): + if not redis_status(): + await context.edit(f"{lang('error_prefix')}{lang('redis_dis')}") + return + if len(context.parameter) != 1: + await context.edit(f"{lang('error_prefix')}{lang('arg_error')}") + return + myself = await context.client.get_me() + self_user_id = myself.id + if context.parameter[0] == "false": + if context.chat_id == self_user_id: + await context.edit(lang('ghost_e_mark')) + return + redis.delete(f"sub.chat_id.{context.chat_id}") + await context.edit(f'成功关闭强制关注频道功能。') + elif context.parameter[0] == "status": + if redis.get(f"sub.chat_id.{context.chat_id}"): + join_chat = redis.get(f"sub.chat_id.{context.chat_id}").decode() + await context.edit(f'当前群组强制需要关注频道的频道为: @{join_chat} 。') + else: + await context.edit('当前群组未开启强制关注频道功能。') + else: + if context.chat_id == self_user_id: + await context.edit(lang('ghost_e_mark')) + return + sub_channel = context.parameter[0].replace('@', '') + redis.set(f"sub.chat_id.{context.chat_id}", sub_channel) + await context.edit(f'已设置当前群组强制需要关注频道的频道为: @{sub_channel} 。') diff --git a/list.json b/list.json index 27efce0..80edcf8 100644 --- a/list.json +++ b/list.json @@ -609,6 +609,16 @@ "supported": true, "des-short": "查询/解除群组中被所回复用户所封禁的用户。", "des": "查询/解除群组中被所回复用户所封禁的用户。命令:unbanby 。" + }, + { + "name": "forcesubscribe", + "version": "1.0", + "section": "daily", + "maintainer": "xtaodada", + "size": "4.2 kb", + "supported": true, + "des-short": "自动删除未关注指定公开频道的用户的发言。", + "des": "自动删除未关注指定公开频道的用户的发言。命令:forcesub 。" } ] }