flood.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """
  2. MIT License
  3. Copyright (c) 2024 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from asyncio import get_running_loop, sleep
  21. from datetime import datetime, timedelta
  22. from pyrogram import filters
  23. from pyrogram.types import (
  24. CallbackQuery,
  25. ChatPermissions,
  26. InlineKeyboardButton,
  27. InlineKeyboardMarkup,
  28. Message,
  29. )
  30. from wbb import SUDOERS, app
  31. from wbb.core.decorators.errors import capture_err
  32. from wbb.core.decorators.permissions import adminsOnly
  33. from wbb.modules.admin import list_admins, member_permissions
  34. from wbb.utils.dbfunctions import flood_off, flood_on, is_flood_on
  35. from wbb.utils.filter_groups import flood_group
  36. __MODULE__ = "防刷屏"
  37. __HELP__ = """
  38. 防刷屏开启后,连续发送超过 10 条消息的普通成员会被禁言一小时,
  39. 管理员不受此规则影响。
  40. /flood [开启|关闭] - 开启或关闭防刷屏;也兼容 enable 和 disable。
  41. """
  42. DB = {} # TODO Use mongodb instead of a fucking dict.
  43. def reset_flood(chat_id, user_id=0):
  44. for user in DB[chat_id].keys():
  45. if user != user_id:
  46. DB[chat_id][user] = 0
  47. @app.on_message(
  48. ~filters.service
  49. & ~filters.me
  50. & ~filters.private
  51. & ~filters.channel
  52. & ~filters.bot,
  53. group=flood_group,
  54. )
  55. @capture_err
  56. async def flood_control_func(_, message: Message):
  57. if not message.chat:
  58. return
  59. chat_id = message.chat.id
  60. if not (await is_flood_on(chat_id)):
  61. return
  62. # Initialize db if not already.
  63. if chat_id not in DB:
  64. DB[chat_id] = {}
  65. if not message.from_user:
  66. reset_flood(chat_id)
  67. return
  68. user_id = message.from_user.id
  69. mention = message.from_user.mention
  70. if user_id not in DB[chat_id]:
  71. DB[chat_id][user_id] = 0
  72. # Reset flood db of current chat if some other user sends a message
  73. reset_flood(chat_id, user_id)
  74. # Ignore devs and admins
  75. mods = await list_admins(chat_id)
  76. if user_id in mods or user_id in SUDOERS:
  77. return
  78. # Mute if user sends more than 10 messages in a row
  79. if DB[chat_id][user_id] >= 10:
  80. DB[chat_id][user_id] = 0
  81. try:
  82. await message.chat.restrict_member(
  83. user_id,
  84. permissions=ChatPermissions(),
  85. until_date=datetime.now() + timedelta(minutes=60),
  86. )
  87. except Exception:
  88. return
  89. keyboard = InlineKeyboardMarkup(
  90. [
  91. [
  92. InlineKeyboardButton(
  93. text="解除禁言",
  94. callback_data=f"unmute_{user_id}",
  95. )
  96. ]
  97. ]
  98. )
  99. m = await message.reply_text(
  100. f"{mention} 连续发送消息触发防刷屏,已被禁言 1 小时。",
  101. reply_markup=keyboard,
  102. )
  103. async def delete():
  104. await sleep(3600)
  105. try:
  106. await m.delete()
  107. except Exception:
  108. pass
  109. loop = get_running_loop()
  110. return loop.create_task(delete())
  111. DB[chat_id][user_id] += 1
  112. @app.on_callback_query(filters.regex("unmute_"))
  113. async def flood_callback_func(_, cq: CallbackQuery):
  114. from_user = cq.from_user
  115. permissions = await member_permissions(cq.message.chat.id, from_user.id)
  116. permission = "can_restrict_members"
  117. if permission not in permissions:
  118. return await cq.answer(
  119. "你没有执行此操作所需的限制成员权限。",
  120. show_alert=True,
  121. )
  122. user_id = cq.data.split("_")[1]
  123. await cq.message.chat.unban_member(user_id)
  124. text = cq.message.text.markdown
  125. text = f"~~{text}~~\n\n"
  126. text += f"__{from_user.mention} 已解除该成员禁言。__"
  127. await cq.message.edit(text)
  128. @app.on_message(filters.command("flood") & ~filters.private)
  129. @adminsOnly("can_change_info")
  130. async def flood_toggle(_, message: Message):
  131. if len(message.command) != 2:
  132. return await message.reply_text("用法:/flood [开启|关闭]")
  133. status = message.text.split(None, 1)[1].strip()
  134. status = status.lower()
  135. status = {
  136. "开启": "enable",
  137. "打开": "enable",
  138. "关闭": "disable",
  139. }.get(status, status)
  140. chat_id = message.chat.id
  141. if status == "enable":
  142. await flood_on(chat_id)
  143. await message.reply_text("防刷屏已开启。")
  144. elif status == "disable":
  145. await flood_off(chat_id)
  146. await message.reply_text("防刷屏已关闭。")
  147. else:
  148. await message.reply_text("参数无效,用法:/flood [开启|关闭]")