spam.py 7.7 KB

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