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