flood.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 pyrogram import filters
  21. from pyrogram.types import (
  22. Message,
  23. ChatPermissions,
  24. CallbackQuery,
  25. InlineKeyboardMarkup,
  26. InlineKeyboardButton,
  27. )
  28. from time import time
  29. from wbb import app, SUDOERS
  30. from wbb.core.decorators.errors import capture_err
  31. from wbb.modules.admin import list_admins, member_permissions
  32. from wbb.utils.filter_groups import flood_group
  33. __MODULE__ = "Flood"
  34. __HELP__ = """
  35. Anti-Flood system, the one who sends more than 7 messages in a row, gets muted for an hour.
  36. Except for admins ofc, and no, you can't change the number of messages or action type.
  37. """
  38. DB = {} # NOTE Use mongodb instead of a fucking dict.
  39. @app.on_message(
  40. ~filters.service
  41. & ~filters.me
  42. & ~filters.private
  43. & ~filters.channel
  44. & ~filters.edited,
  45. group=flood_group,
  46. )
  47. @capture_err
  48. async def flood_control_func(_, message: Message):
  49. if not message.from_user:
  50. return
  51. user_id = message.from_user.id
  52. mention = message.from_user.mention
  53. chat_id = message.chat.id
  54. # Ignore devs and admins
  55. mods = (await list_admins(chat_id)) + SUDOERS
  56. if user_id in mods:
  57. return
  58. # Initialize db if not already.
  59. if chat_id not in DB:
  60. DB[chat_id] = {}
  61. if user_id not in DB[chat_id]:
  62. DB[chat_id][user_id] = 0
  63. # Reset every other user's flood count if someone else sends a message.
  64. for user in DB[chat_id].keys():
  65. if user != user_id:
  66. DB[chat_id][user] = 0
  67. # Mute if user sends more than 7 messages in a row
  68. if DB[chat_id][user_id] >= 7:
  69. DB[chat_id][user_id] = 0
  70. try:
  71. await message.chat.restrict_member(
  72. user_id,
  73. permissions=ChatPermissions(),
  74. until_date=int(time() + 3600),
  75. )
  76. except Exception:
  77. return
  78. keyboard = InlineKeyboardMarkup(
  79. [
  80. [
  81. InlineKeyboardButton(
  82. text="🚨 Unmute 🚨",
  83. callback_data=f"unmute_{user_id}",
  84. )
  85. ]
  86. ]
  87. )
  88. return await message.reply_text(
  89. f"Imagine flooding the chat in front of me, Muted {mention} for an hour!",
  90. reply_markup=keyboard,
  91. )
  92. DB[chat_id][user_id] += 1
  93. @app.on_callback_query(filters.regex("unmute_"))
  94. async def flood_callback_func(_, cq: CallbackQuery):
  95. from_user = cq.from_user
  96. permissions = await member_permissions(cq.message.chat.id, from_user.id)
  97. permission = "can_restrict_members"
  98. if permission not in permissions:
  99. return await cq.answer(
  100. "You don't have enough permissions to perform this action.\n"
  101. + f"Permission needed: {permission}",
  102. show_alert=True,
  103. )
  104. user_id = cq.data.split("_")[1]
  105. await cq.message.chat.unban_member(user_id)
  106. text = cq.message.text.markdown
  107. text = f"~~{text}~~\n\n"
  108. text += f"__User unmuted by {from_user.mention}__"
  109. await cq.message.edit(text)