""" MIT License Copyright (c) 2024 TheHamkerCat Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from asyncio import get_running_loop, sleep from datetime import datetime, timedelta from pyrogram import filters from pyrogram.types import ( CallbackQuery, ChatPermissions, InlineKeyboardButton, InlineKeyboardMarkup, Message, ) from wbb import SUDOERS, app from wbb.core.decorators.errors import capture_err from wbb.core.decorators.permissions import adminsOnly from wbb.modules.admin import list_admins, member_permissions from wbb.utils.dbfunctions import flood_off, flood_on, is_flood_on from wbb.utils.filter_groups import flood_group __MODULE__ = "防刷屏" __HELP__ = """ 防刷屏开启后,连续发送超过 10 条消息的普通成员会被禁言一小时, 管理员不受此规则影响。 /flood [开启|关闭] - 开启或关闭防刷屏;也兼容 enable 和 disable。 """ DB = {} # TODO Use mongodb instead of a fucking dict. def reset_flood(chat_id, user_id=0): for user in DB[chat_id].keys(): if user != user_id: DB[chat_id][user] = 0 @app.on_message( ~filters.service & ~filters.me & ~filters.private & ~filters.channel & ~filters.bot, group=flood_group, ) @capture_err async def flood_control_func(_, message: Message): if not message.chat: return chat_id = message.chat.id if not (await is_flood_on(chat_id)): return # Initialize db if not already. if chat_id not in DB: DB[chat_id] = {} if not message.from_user: reset_flood(chat_id) return user_id = message.from_user.id mention = message.from_user.mention if user_id not in DB[chat_id]: DB[chat_id][user_id] = 0 # Reset flood db of current chat if some other user sends a message reset_flood(chat_id, user_id) # Ignore devs and admins mods = await list_admins(chat_id) if user_id in mods or user_id in SUDOERS: return # Mute if user sends more than 10 messages in a row if DB[chat_id][user_id] >= 10: DB[chat_id][user_id] = 0 try: await message.chat.restrict_member( user_id, permissions=ChatPermissions(), until_date=datetime.now() + timedelta(minutes=60), ) except Exception: return keyboard = InlineKeyboardMarkup( [ [ InlineKeyboardButton( text="解除禁言", callback_data=f"unmute_{user_id}", ) ] ] ) m = await message.reply_text( f"{mention} 连续发送消息触发防刷屏,已被禁言 1 小时。", reply_markup=keyboard, ) async def delete(): await sleep(3600) try: await m.delete() except Exception: pass loop = get_running_loop() return loop.create_task(delete()) DB[chat_id][user_id] += 1 @app.on_callback_query(filters.regex("unmute_")) async def flood_callback_func(_, cq: CallbackQuery): from_user = cq.from_user permissions = await member_permissions(cq.message.chat.id, from_user.id) permission = "can_restrict_members" if permission not in permissions: return await cq.answer( "你没有执行此操作所需的限制成员权限。", show_alert=True, ) user_id = cq.data.split("_")[1] await cq.message.chat.unban_member(user_id) text = cq.message.text.markdown text = f"~~{text}~~\n\n" text += f"__{from_user.mention} 已解除该成员禁言。__" await cq.message.edit(text) @app.on_message(filters.command("flood") & ~filters.private) @adminsOnly("can_change_info") async def flood_toggle(_, message: Message): if len(message.command) != 2: return await message.reply_text("用法:/flood [开启|关闭]") status = message.text.split(None, 1)[1].strip() status = status.lower() status = { "开启": "enable", "打开": "enable", "关闭": "disable", }.get(status, status) chat_id = message.chat.id if status == "enable": await flood_on(chat_id) await message.reply_text("防刷屏已开启。") elif status == "disable": await flood_off(chat_id) await message.reply_text("防刷屏已关闭。") else: await message.reply_text("参数无效,用法:/flood [开启|关闭]")