forked from TeamPGM/PagerMaid_Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forcegroup.py
87 lines (83 loc) · 3.81 KB
/
forcegroup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from pagermaid import bot, redis, redis_status, version
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:
if context.sender.bot:
pass
except AttributeError:
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()
try:
reply = reply.id
except AttributeError:
reply = None
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)
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="<true|false|status>")
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'成功在当前群组开启强制加入频道讨论群功能。')