spam.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 (CallbackQuery, InlineKeyboardButton,
  22. InlineKeyboardMarkup, Message)
  23. from wbb import LOG_GROUP_ID, SUDOERS, app
  24. from wbb.core.decorators.permissions import adminsOnly
  25. from wbb.modules.admin import list_admins, member_permissions
  26. from wbb.modules.trust import get_spam_data
  27. from wbb.utils.dbfunctions import (is_spam_detection_on,
  28. spam_detection_off,
  29. spam_detection_on)
  30. from wbb.utils.filter_groups import spam_protection_group
  31. __MODULE__ = "AntiSpam"
  32. __HELP__ = """
  33. **Antispam helps protect your group from spam.**
  34. - It's still new so it might be a bit inaccurate.
  35. - Currently, it only alerts for spam and gives you a button to delete the message,
  36. but in the future, it will delete the spam message automatically.
  37. **Commands:**
  38. - /spam
  39. To mark a message as spam, this will help devs to
  40. improve spam protection algorithm.
  41. - /spam_detection [ENABLE|DISABLE]
  42. To turn off spam detection in your chat.
  43. **Plot Twist:** NO, WE'RE NOT USING INTELLIVOID'S SPAM DETECTION API!
  44. """
  45. @app.on_message(
  46. (filters.text | filters.caption) & ~filters.private & ~filters.me,
  47. group=spam_protection_group,
  48. )
  49. async def spam_protection_func(_, message: Message):
  50. text = message.text or message.caption
  51. chat_id = message.chat.id
  52. user = message.from_user
  53. if not text or not user:
  54. return
  55. # We'll handle admins only if it's spam, ignore only sudo users for now.
  56. if user.id in SUDOERS:
  57. return
  58. enabled = await is_spam_detection_on(chat_id)
  59. if not enabled:
  60. return
  61. data = await get_spam_data(message, text)
  62. if isinstance(data, str):
  63. return
  64. if not data.is_spam:
  65. return
  66. if user.id in (await list_admins(chat_id)):
  67. return
  68. text = f"""
  69. 🚨 **SPAM DETECTED** 🚨
  70. **User:** {user.mention}
  71. **Message:** [Link]({message.link})
  72. **Spam Probability:** {data.spam_probability} %
  73. **Action:** Alerted
  74. """
  75. keyboard = InlineKeyboardMarkup(
  76. [
  77. [
  78. InlineKeyboardButton(
  79. text="Yes it's spam", callback_data="s_p_spam"
  80. ),
  81. InlineKeyboardButton(
  82. text="No, it's not spam",
  83. callback_data="s_p_ham",
  84. ),
  85. ],
  86. ]
  87. )
  88. await message.reply_text(
  89. text=text,
  90. reply_markup=keyboard,
  91. disable_web_page_preview=True,
  92. )
  93. dev_forward = (
  94. "If you're not a developer of WBB, forward this message to devs, "
  95. + "so that they can use it to improve spam protection algorithm."
  96. )
  97. @app.on_callback_query(filters.regex("s_p_"))
  98. async def spam_p_callback(_, cq: CallbackQuery):
  99. from_user = cq.from_user
  100. chat_id = cq.message.chat.id
  101. permissions = await member_permissions(chat_id, from_user.id)
  102. permission = "can_delete_messages"
  103. if permission not in permissions:
  104. return await cq.answer(
  105. "You don't have enough permissions to perform this action.\n"
  106. + f"Permission needed: {permission}",
  107. show_alert=True,
  108. )
  109. if cq.data.split("_")[-1] == "spam":
  110. try:
  111. await cq.message.reply_to_message.delete()
  112. except Exception:
  113. await cq.message.delete()
  114. return
  115. text = cq.message.text.markdown
  116. text = text.replace(
  117. "Alerted",
  118. f"Deleted Message With {from_user.mention}'s Approval.",
  119. )
  120. await cq.message.edit(text)
  121. if cq.message.reply_to_message:
  122. text = f"**ADMINS OF {chat_id} FLAGGED THIS MESSAGE AS SPAM**\n\n"
  123. text += f"`{cq.message.reply_to_message.text.markdown}`\n\n__{dev_forward}__"
  124. return await app.send_message(
  125. LOG_GROUP_ID, text, disable_web_page_preview=True
  126. )
  127. return
  128. await cq.message.delete()
  129. if cq.message.reply_to_message:
  130. text = f"**ADMINS OF {chat_id} FLAGGED THIS MESSAGE AS NOT SPAM**\n\n"
  131. text += f"`{cq.message.reply_to_message.text.markdown}`\n\n__{dev_forward}__"
  132. return await app.send_message(
  133. LOG_GROUP_ID, text, disable_web_page_preview=True
  134. )
  135. @app.on_message(filters.command("spam") & ~filters.private)
  136. async def spam_flag_func(_, message: Message):
  137. if not message.reply_to_message:
  138. return await message.reply_text(
  139. "Reply to a message to flag it as spam"
  140. )
  141. r = message.reply_to_message
  142. if not r.text and not r.caption:
  143. return await message.reply_text(
  144. "Reply to a text message to flag it as spam"
  145. )
  146. text = r.text or r.caption
  147. if not text:
  148. return await message.reply_text(
  149. "Reply to a text message to flag it as spam"
  150. )
  151. msg = f"""
  152. **ADMINS OF {message.chat.id} FLAGGED THIS MESSAGE AS SPAM. [Suggestion]**
  153. ```
  154. {text.markdown}
  155. ```
  156. __{dev_forward}__
  157. """
  158. await message.reply_text(
  159. "Message was flagged as spam, Devs will use it to improve spam protection algorithm."
  160. )
  161. await app.send_message(
  162. LOG_GROUP_ID, msg, disable_web_page_preview=True
  163. )
  164. @app.on_message(filters.command("spam_detection") & ~filters.private)
  165. @adminsOnly("can_change_info")
  166. async def spam_toggle(_, message: Message):
  167. if len(message.command) != 2:
  168. return await message.reply_text(
  169. "Usage: /spam_detection [ENABLE|DISABLE]"
  170. )
  171. status = message.text.split(None, 1)[1].strip()
  172. status = status.lower()
  173. chat_id = message.chat.id
  174. if status == "enable":
  175. await spam_detection_on(chat_id)
  176. await message.reply_text("Enabled Spam Detection System.")
  177. elif status == "disable":
  178. await spam_detection_off(chat_id)
  179. await message.reply_text("Disabled Spam Detection System.")
  180. else:
  181. await message.reply_text(
  182. "Unknown Suffix, Use /spam_detection [ENABLE|DISABLE]"
  183. )