flood.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """
  2. MIT License
  3. Copyright (c) 2021 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 time import time
  22. from pyrogram import filters
  23. from pyrogram.types import (CallbackQuery, ChatPermissions,
  24. InlineKeyboardButton,
  25. InlineKeyboardMarkup, Message)
  26. from wbb import SUDOERS, app
  27. from wbb.core.decorators.errors import capture_err
  28. from wbb.core.decorators.permissions import adminsOnly
  29. from wbb.modules.admin import list_admins, member_permissions
  30. from wbb.utils.dbfunctions import flood_off, flood_on, is_flood_on
  31. from wbb.utils.filter_groups import flood_group
  32. __MODULE__ = "Flood"
  33. __HELP__ = """
  34. Anti-Flood system, the one who sends more than 10 messages in a row, gets muted for an hour (Except for admins).
  35. /flood [ENABLE|DISABLE] - Turn flood detection on or off
  36. """
  37. DB = {} # TODO Use mongodb instead of a fucking dict.
  38. def reset_flood(chat_id, user_id=0):
  39. for user in DB[chat_id].keys():
  40. if user != user_id:
  41. DB[chat_id][user] = 0
  42. @app.on_message(
  43. ~filters.service
  44. & ~filters.me
  45. & ~filters.private
  46. & ~filters.channel
  47. & ~filters.bot
  48. & ~filters.edited,
  49. group=flood_group,
  50. )
  51. @capture_err
  52. async def flood_control_func(_, message: Message):
  53. if not message.chat:
  54. return
  55. chat_id = message.chat.id
  56. if not (await is_flood_on(chat_id)):
  57. return
  58. # Initialize db if not already.
  59. if chat_id not in DB:
  60. DB[chat_id] = {}
  61. if not message.from_user:
  62. reset_flood(chat_id)
  63. return
  64. user_id = message.from_user.id
  65. mention = message.from_user.mention
  66. if user_id not in DB[chat_id]:
  67. DB[chat_id][user_id] = 0
  68. # Reset floodb of current chat if some other user sends a message
  69. reset_flood(chat_id, user_id)
  70. # Ignore devs and admins
  71. mods = (await list_admins(chat_id)) + SUDOERS
  72. if user_id in mods:
  73. return
  74. # Mute if user sends more than 10 messages in a row
  75. if DB[chat_id][user_id] >= 10:
  76. DB[chat_id][user_id] = 0
  77. try:
  78. await message.chat.restrict_member(
  79. user_id,
  80. permissions=ChatPermissions(),
  81. until_date=int(time() + 3600),
  82. )
  83. except Exception:
  84. return
  85. keyboard = InlineKeyboardMarkup(
  86. [
  87. [
  88. InlineKeyboardButton(
  89. text="🚨 Unmute 🚨",
  90. callback_data=f"unmute_{user_id}",
  91. )
  92. ]
  93. ]
  94. )
  95. m = await message.reply_text(
  96. f"Imagine flooding the chat in front of me, Muted {mention} for an hour!",
  97. reply_markup=keyboard,
  98. )
  99. async def delete():
  100. await sleep(3600)
  101. try:
  102. await m.delete()
  103. except Exception:
  104. pass
  105. loop = get_running_loop()
  106. return loop.create_task(delete())
  107. DB[chat_id][user_id] += 1
  108. @app.on_callback_query(filters.regex("unmute_"))
  109. async def flood_callback_func(_, cq: CallbackQuery):
  110. from_user = cq.from_user
  111. permissions = await member_permissions(
  112. cq.message.chat.id, from_user.id
  113. )
  114. permission = "can_restrict_members"
  115. if permission not in permissions:
  116. return await cq.answer(
  117. "You don't have enough permissions to perform this action.\n"
  118. + f"Permission needed: {permission}",
  119. show_alert=True,
  120. )
  121. user_id = cq.data.split("_")[1]
  122. await cq.message.chat.unban_member(user_id)
  123. text = cq.message.text.markdown
  124. text = f"~~{text}~~\n\n"
  125. text += f"__User unmuted by {from_user.mention}__"
  126. await cq.message.edit(text)
  127. @app.on_message(filters.command("flood") & ~filters.private)
  128. @adminsOnly("can_change_info")
  129. async def flood_toggle(_, message: Message):
  130. if len(message.command) != 2:
  131. return await message.reply_text(
  132. "Usage: /flood [ENABLE|DISABLE]"
  133. )
  134. status = message.text.split(None, 1)[1].strip()
  135. status = status.lower()
  136. chat_id = message.chat.id
  137. if status == "enable":
  138. await flood_on(chat_id)
  139. await message.reply_text("Enabled Flood Checker.")
  140. elif status == "disable":
  141. await flood_off(chat_id)
  142. await message.reply_text("Disabled Flood Checker.")
  143. else:
  144. await message.reply_text(
  145. "Unknown Suffix, Use /flood [ENABLE|DISABLE]"
  146. )