global_stats.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from wbb import app
  2. from wbb.utils.filter_groups import global_stats_group
  3. from wbb.utils.errors import capture_err
  4. from wbb.utils.botinfo import BOT_ID, BOT_NAME
  5. from wbb.utils.fetch import fetch
  6. from wbb.utils.dbfunctions import (
  7. is_served_chat,
  8. get_served_chats,
  9. add_served_chat,
  10. remove_served_chat,
  11. get_notes_count,
  12. get_filters_count,
  13. get_warns_count,
  14. get_karmas_count,
  15. get_gbans_count
  16. )
  17. from pyrogram import filters
  18. from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant, PeerIdInvalid
  19. @app.on_message(filters.text, group=global_stats_group)
  20. @capture_err
  21. async def chat_watcher(_, message):
  22. chat_id = message.chat.id
  23. served_chat = await is_served_chat(chat_id)
  24. if served_chat: return
  25. await add_served_chat(chat_id)
  26. async def global_stats(_, message):
  27. m = await app.send_message(
  28. message.chat.id,
  29. text="__**Analysing Stats, Might Take 10-30 Seconds.**__",
  30. disable_web_page_preview=True
  31. )
  32. ## For bot served chat and users count
  33. served_chats = []
  34. total_users = 0
  35. chats = await get_served_chats()
  36. for chat in chats: served_chats.append(chat["chat_id"])
  37. for served_chat in served_chats:
  38. try:
  39. await app.get_chat_member(int(served_chat), BOT_ID)
  40. except (UserNotParticipant, PeerIdInvalid) as e:
  41. await remove_served_chat(served_chat)
  42. served_chats.remove(served_chat)
  43. pass
  44. for i in served_chats:
  45. mc = (await app.get_chat(i)).members_count
  46. total_users += int(mc)
  47. # Gbans count
  48. gbans = await get_gbans_count()
  49. ## Notes count across chats
  50. _notes = await get_notes_count()
  51. notes_count = _notes["notes_count"]
  52. notes_chats_count = _notes["chats_count"]
  53. # Filters count across chats
  54. _filters = await get_filters_count()
  55. filters_count = _filters["filters_count"]
  56. filters_chats_count = _filters["chats_count"]
  57. # Warns count across chats
  58. _warns = await get_warns_count()
  59. warns_count = _warns["warns_count"]
  60. warns_chats_count = _warns["chats_count"]
  61. # Karmas count across chats
  62. _karmas = await get_karmas_count()
  63. karmas_count = _karmas["karmas_count"]
  64. karmas_chats_count = _karmas["chats_count"]
  65. # Contributors/Developers count and commits on github
  66. url = "https://api.github.com/repos/thehamkercat/williambutcherbot/contributors"
  67. rurl = "https://github.com/thehamkercat/williambutcherbot"
  68. developers = await fetch(url)
  69. commits = 0
  70. for developer in developers:
  71. commits += developer['contributions']
  72. developers = len(developers)
  73. # NOTE need to fix these
  74. msg = f"""**Global Stats of {BOT_NAME}**:
  75. **{gbans}** Globally banned users.
  76. **{total_users}** Users, Across **{len(served_chats)}** chats.
  77. **{filters_count}** Filters, Across **{filters_chats_count}** chats.
  78. **{notes_count}** Notes, Across **{notes_chats_count}** chats.
  79. **{warns_count}** Warns, Across **{warns_chats_count}** chats.
  80. **{karmas_count}** Karma, Across **{karmas_chats_count}** chats.
  81. **{developers}** Developers And **{commits}** Commits On **[Github]({rurl})**."""
  82. await m.edit(msg, disable_web_page_preview=True)