chat_watcher.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. MIT License
  3. Copyright (c) 2024 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 wbb import app
  21. from wbb.services.member_identity import observe_message_member_identities
  22. from wbb.utils.dbadmin import upsert_managed_chat
  23. from wbb.utils.dbfunctions import (
  24. add_served_chat,
  25. add_served_user,
  26. blacklisted_chats,
  27. )
  28. from wbb.utils.filter_groups import chat_watcher_group
  29. @app.on_message(group=chat_watcher_group)
  30. async def chat_watcher_func(_, message):
  31. if message.from_user:
  32. user_id = message.from_user.id
  33. await add_served_user(user_id)
  34. chat_id = message.chat.id
  35. blacklisted_chats_list = await blacklisted_chats()
  36. if not chat_id:
  37. return
  38. if chat_id in blacklisted_chats_list:
  39. return await app.leave_chat(chat_id)
  40. await add_served_chat(chat_id)
  41. if chat_id < 0:
  42. await observe_message_member_identities(chat_id=chat_id, message=message)
  43. await upsert_managed_chat(
  44. chat_id=chat_id,
  45. title=message.chat.title,
  46. username=message.chat.username,
  47. chat_type=str(getattr(message.chat.type, "value", message.chat.type)),
  48. )