trust.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 sleep
  21. from pyrogram import filters
  22. from pyrogram.types import Message
  23. from wbb import (BOT_ID, MESSAGE_DUMP_CHAT, USERBOT_BOT_CHAT_COMMON, app,
  24. app2, arq)
  25. from wbb.utils.dbfunctions import get_trust_db, update_trust_db
  26. from wbb.utils.filter_groups import trust_group
  27. spam_db = {}
  28. async def get_spam_data(message: Message, text: str):
  29. c, m = message.chat.id, message.message_id
  30. if c not in spam_db:
  31. spam_db[c] = {}
  32. if m not in spam_db[c]:
  33. data = (await arq.nlp(text)).result[0]
  34. spam_db[c][m] = data
  35. return spam_db[c][m]
  36. @app2.on_message(
  37. (filters.text | filters.caption)
  38. & ~filters.chat(BOT_ID)
  39. & ~filters.chat(USERBOT_BOT_CHAT_COMMON)
  40. & ~filters.me,
  41. group=trust_group,
  42. )
  43. @app.on_message(
  44. (filters.text | filters.caption)
  45. & ~filters.chat(MESSAGE_DUMP_CHAT)
  46. & ~filters.me
  47. & ~filters.private,
  48. group=trust_group,
  49. )
  50. async def trust_watcher_func(_, message: Message):
  51. # Sleeping so that we can get cached data created by
  52. # spam.py with get_spam_data function
  53. if message.command:
  54. return
  55. if not message.from_user:
  56. return
  57. user_id = message.from_user.id
  58. text = message.text or message.caption
  59. text = text.strip()
  60. if not text:
  61. return
  62. if len(text) < 2:
  63. return
  64. data = await get_spam_data(message, text)
  65. spam = data.spam
  66. await update_trust_db(user_id, spam)
  67. async def get_spam_probability(user_id) -> list:
  68. data = await get_trust_db(user_id)
  69. if not data:
  70. return [0, 0]
  71. mean = lambda x: sum(x) / len(x)
  72. spam = [i for i in data]
  73. return [mean(spam), len(spam)]