From 48c416ff3e31f85f9c88cda477d11fa02cfcc0f1 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Thu, 15 Jul 2021 21:29:32 +0800 Subject: [PATCH] =?UTF-8?q?forcegroup=20=E5=BC=BA=E5=88=B6=E9=A2=91?= =?UTF-8?q?=E9=81=93=E7=94=A8=E6=88=B7=E5=8A=A0=E5=85=A5=E9=A2=91=E9=81=93?= =?UTF-8?q?=E8=AE=A8=E8=AE=BA=E7=BE=A4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- forcegroup.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++ forcesubscribe.py | 2 +- list.json | 10 ++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 forcegroup.py diff --git a/forcegroup.py b/forcegroup.py new file mode 100644 index 0000000..5594b50 --- /dev/null +++ b/forcegroup.py @@ -0,0 +1,78 @@ +from pagermaid import bot, redis, redis_status +from pagermaid.utils import lang, alias_command +from pagermaid.listener import listener +from asyncio import sleep +from telethon.tl.custom.message import Message +from telethon.tl.functions.channels import GetParticipantRequest +from telethon.errors.rpcerrorlist import UserNotParticipantError, ChatAdminRequiredError +from telethon.tl.types import ChannelParticipantsAdmins + + +@listener(is_plugin=True, incoming=True, ignore_edited=True) +async def force_group_msg(context: Message): + if not redis_status(): + return + join_chat = redis.get(f"group.chat_id.{context.chat_id}") + if not join_chat: + return + if redis.get(f"group.chat_id.{context.chat_id}.{context.sender_id}"): + return + try: + try: + await bot(GetParticipantRequest(context.chat, context.sender_id)) + except ValueError: + await bot.get_participants(context.chat) + await bot(GetParticipantRequest(context.chat, context.sender_id)) + redis.set(f'group.chat_id.{context.chat_id}.{context.sender_id}', 'true') + redis.expire(f'group.chat_id.{context.chat_id}.{context.sender_id}', 3600) + except UserNotParticipantError: + try: + reply = await context.get_reply_message() + await context.delete() + msg = await bot.send_message(context.chat_id, + f'[{context.sender.first_name}](tg://user?id={context.sender_id}) ' + f'您需要先加入频道讨论群才能发言。', + reply_to=reply.id) + await sleep(5) + await msg.delete() + except ChatAdminRequiredError: + redis.delete(f"group.chat_id.{context.chat_id}") + except ChatAdminRequiredError: + redis.delete(f"group.chat_id.{context.chat_id}") + + +@listener(is_plugin=True, outgoing=True, command=alias_command('forcegroup'), + description='自动删除未加入频道讨论群用户的发言。', + parameters="") +async def force_group(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"group.chat_id.{context.chat_id}") + await context.edit(f'成功关闭强制加入频道讨论群功能。') + elif context.parameter[0] == "status": + if redis.get(f"group.chat_id.{context.chat_id}"): + await context.edit(f'当前群组已开启强制加入频道讨论群功能。') + else: + await context.edit('当前群组未开启强制加入频道讨论群功能。') + else: + if context.chat_id == self_user_id: + await context.edit(lang('ghost_e_mark')) + return + admins = await context.client.get_participants(context.chat, filter=ChannelParticipantsAdmins) + if context.sender in admins: + user = admins[admins.index(context.sender)] + if not user.participant.admin_rights.delete_messages: + await context.edit('无删除消息权限,拒绝开启此功能。') + return + redis.set(f"group.chat_id.{context.chat_id}", 'true') + await context.edit(f'成功在当前群组开启强制加入频道讨论群功能。') diff --git a/forcesubscribe.py b/forcesubscribe.py index 0bb2eea..cd9c11c 100644 --- a/forcesubscribe.py +++ b/forcesubscribe.py @@ -80,7 +80,7 @@ async def force_subscribe_msg(context: Message): redis.delete(f"sub.chat_id.{context.chat_id}") -@listener(is_plugin=False, outgoing=True, command=alias_command('forcesub'), +@listener(is_plugin=True, outgoing=True, command=alias_command('forcesub'), description='自动删除未关注指定公开频道的用户的发言。', parameters="") async def force_sub(context): diff --git a/list.json b/list.json index c897392..b6a69fb 100644 --- a/list.json +++ b/list.json @@ -619,6 +619,16 @@ "supported": true, "des-short": "自动删除未关注指定公开频道的用户的发言。", "des": "自动删除未关注指定公开频道的用户的发言。命令:forcesub 。" + }, + { + "name": "forcegroup", + "version": "1.0", + "section": "daily", + "maintainer": "xtaodada", + "size": "3.6 kb", + "supported": true, + "des-short": "强制加入频道讨论群。", + "des": "自动删除未加入频道讨论群用户的发言。命令:forcegroup 。" } ] }