2022-07-26 07:32:55 +00:00
|
|
|
import contextlib
|
|
|
|
from asyncio import sleep
|
2022-09-15 06:41:16 +00:00
|
|
|
from random import uniform
|
2022-07-26 07:32:55 +00:00
|
|
|
|
|
|
|
from pyrogram.enums import ChatMemberStatus
|
|
|
|
from pyrogram import filters
|
2023-07-01 12:18:58 +00:00
|
|
|
from pyrogram.errors import (
|
|
|
|
ChatAdminRequired,
|
|
|
|
FloodWait,
|
|
|
|
UserAdminInvalid,
|
|
|
|
PeerIdInvalid,
|
|
|
|
BadRequest,
|
|
|
|
)
|
2022-07-26 07:32:55 +00:00
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
from pagermaid.listener import listener
|
|
|
|
from pagermaid.enums import Message, Client
|
|
|
|
from pagermaid.services import bot
|
|
|
|
|
|
|
|
|
|
|
|
async def check_self_and_from(message: Message):
|
|
|
|
cid = message.chat.id
|
|
|
|
data = await bot.get_chat_member(cid, (await bot.get_me()).id)
|
|
|
|
if data.status not in [ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER]:
|
|
|
|
return False
|
|
|
|
if not message.from_user:
|
|
|
|
return False
|
2022-08-27 12:27:35 +00:00
|
|
|
if message.outgoing:
|
|
|
|
return True
|
2022-07-26 07:32:55 +00:00
|
|
|
data = await bot.get_chat_member(cid, message.from_user.id)
|
2022-08-01 17:01:08 +00:00
|
|
|
return data.status in [ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER]
|
2022-07-26 07:32:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def kick_chat_member(cid, uid, only_search: bool = False):
|
|
|
|
if only_search:
|
|
|
|
return
|
2022-09-15 06:41:16 +00:00
|
|
|
try:
|
|
|
|
with contextlib.suppress(UserAdminInvalid, PeerIdInvalid, BadRequest):
|
2023-07-01 12:18:58 +00:00
|
|
|
await bot.ban_chat_member(cid, uid, datetime.now() + timedelta(minutes=5))
|
2022-09-15 06:41:16 +00:00
|
|
|
except FloodWait as e:
|
|
|
|
await sleep(e.value + uniform(0.5, 1.0))
|
|
|
|
await kick_chat_member(cid, uid, only_search)
|
2022-07-26 07:32:55 +00:00
|
|
|
|
|
|
|
|
2023-07-01 12:18:58 +00:00
|
|
|
async def process_clean_member(
|
|
|
|
message: Message, mode: str, day: int, only_search: bool = False
|
|
|
|
):
|
2022-07-26 07:32:55 +00:00
|
|
|
member_count = 0
|
|
|
|
try:
|
|
|
|
async for member in bot.get_chat_members(message.chat.id):
|
2023-07-01 12:18:58 +00:00
|
|
|
if (
|
|
|
|
mode == "1"
|
|
|
|
and member.user.last_online_date
|
|
|
|
and member.user.last_online_date < datetime.now() - timedelta(days=day)
|
|
|
|
):
|
2022-07-26 07:32:55 +00:00
|
|
|
member_count += 1
|
|
|
|
await kick_chat_member(message.chat.id, member.user.id, only_search)
|
|
|
|
if mode == "2":
|
|
|
|
now = datetime.now() - timedelta(days=day)
|
2023-07-01 12:18:58 +00:00
|
|
|
async for message in bot.search_messages(
|
|
|
|
message.chat.id, limit=1, from_user=member.user.id
|
|
|
|
):
|
2022-07-26 07:32:55 +00:00
|
|
|
if message.date < now:
|
|
|
|
member_count += 1
|
2023-07-01 12:18:58 +00:00
|
|
|
await kick_chat_member(
|
|
|
|
message.chat.id, member.user.id, only_search
|
|
|
|
)
|
2022-07-26 07:32:55 +00:00
|
|
|
elif mode == "3":
|
|
|
|
try:
|
2023-07-01 12:18:58 +00:00
|
|
|
count = await bot.search_messages_count(
|
|
|
|
message.chat.id, from_user=member.user.id
|
|
|
|
)
|
2022-07-26 07:32:55 +00:00
|
|
|
except PeerIdInvalid:
|
|
|
|
continue
|
|
|
|
if count < day:
|
|
|
|
member_count += 1
|
|
|
|
await kick_chat_member(message.chat.id, member.user.id, only_search)
|
|
|
|
if mode == "4" and member.user.is_deleted:
|
|
|
|
member_count += 1
|
|
|
|
await kick_chat_member(message.chat.id, member.user.id, only_search)
|
2022-09-15 06:41:16 +00:00
|
|
|
if mode == "5":
|
|
|
|
member_count += 1
|
|
|
|
await kick_chat_member(message.chat.id, member.user.id, only_search)
|
2022-07-26 07:32:55 +00:00
|
|
|
if not only_search:
|
2023-07-01 12:18:58 +00:00
|
|
|
await message.edit(f"成功清理了 `{member_count}` 人。")
|
2022-07-26 07:32:55 +00:00
|
|
|
else:
|
2023-07-01 12:18:58 +00:00
|
|
|
await message.edit(f"查找到了 `{member_count}` 人。")
|
2022-07-26 07:32:55 +00:00
|
|
|
except ChatAdminRequired:
|
|
|
|
await message.edit("你好像并不拥有封禁用户权限。")
|
|
|
|
except FloodWait:
|
2023-07-01 12:18:58 +00:00
|
|
|
return await message.edit("处理失败,您已受到 TG 服务器限制。")
|
2022-07-26 07:32:55 +00:00
|
|
|
|
|
|
|
|
2023-07-01 12:18:58 +00:00
|
|
|
@listener(
|
|
|
|
command="clean_member", need_admin=True, groups_only=True, description="多种方式清理群成员"
|
|
|
|
)
|
2022-07-26 07:32:55 +00:00
|
|
|
async def clean_member(client: Client, message: Message):
|
|
|
|
if not await check_self_and_from(message):
|
|
|
|
return await message.edit("您不是群管理员,无法使用此命令")
|
|
|
|
uid = message.from_user.id
|
|
|
|
mode, day = "0", 0
|
2023-07-01 12:18:58 +00:00
|
|
|
reply = await message.edit(
|
|
|
|
"请选择清理模式:\n\n"
|
|
|
|
"1. 按未上线时间清理\n"
|
|
|
|
"2. 按未发言时间清理(大群慎用)\n"
|
|
|
|
"3. 按发言数清理\n"
|
|
|
|
"4. 清理死号\n"
|
|
|
|
"5. 清理所有人(大群慎用)"
|
|
|
|
)
|
2022-07-26 07:32:55 +00:00
|
|
|
try:
|
2023-07-01 12:18:58 +00:00
|
|
|
async with client.conversation(
|
|
|
|
message.chat.id, filters=filters.user(uid)
|
|
|
|
) as conv:
|
2022-07-26 07:32:55 +00:00
|
|
|
await sleep(1)
|
|
|
|
res: Message = await conv.get_response()
|
|
|
|
mode = res.text
|
|
|
|
await res.safe_delete()
|
|
|
|
if mode in ["1", "2"]:
|
|
|
|
await res.safe_delete()
|
|
|
|
await reply.edit("请输入清理天数:")
|
|
|
|
await sleep(1)
|
|
|
|
res = await conv.get_response()
|
|
|
|
day = max(int(res.text), 7)
|
|
|
|
await res.safe_delete()
|
|
|
|
elif mode == "3":
|
|
|
|
await res.safe_delete()
|
|
|
|
await reply.edit("清理发言少于多少条的群成员:")
|
|
|
|
await sleep(1)
|
|
|
|
res = await conv.get_response()
|
|
|
|
day = int(res.text)
|
|
|
|
await res.safe_delete()
|
2022-09-15 06:41:16 +00:00
|
|
|
elif mode == "4":
|
|
|
|
pass
|
|
|
|
elif mode != "5":
|
2022-07-26 07:32:55 +00:00
|
|
|
raise ValueError("清理模式错误")
|
|
|
|
await reply.edit("查找还是清理?")
|
|
|
|
await sleep(1)
|
|
|
|
res = await conv.get_response()
|
|
|
|
only_search = res.text == "查找"
|
|
|
|
await res.safe_delete()
|
|
|
|
except ValueError as e:
|
|
|
|
return await reply.edit(f"{e}")
|
|
|
|
await reply.edit("遍历成员中。。。")
|
|
|
|
await process_clean_member(reply, mode, day, only_search)
|