global_stats.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from wbb import app, SUDOERS
  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.modules import ALL_MODULES
  7. from wbb.utils.dbfunctions import (
  8. is_served_chat,
  9. get_served_chats,
  10. add_served_chat,
  11. remove_served_chat,
  12. get_notes_count,
  13. get_filters_count,
  14. get_warns_count,
  15. get_karmas_count,
  16. get_gbans_count
  17. )
  18. from pyrogram import filters
  19. import asyncio
  20. @app.on_message(filters.text, group=global_stats_group)
  21. @capture_err
  22. async def chat_watcher(_, message):
  23. chat_id = message.chat.id
  24. served_chat = await is_served_chat(chat_id)
  25. if served_chat:
  26. return
  27. await add_served_chat(chat_id)
  28. @app.on_message(
  29. filters.command("gstats") & filters.user(SUDOERS)
  30. & ~filters.edited
  31. )
  32. @capture_err
  33. async def global_stats(_, message):
  34. m = await app.send_message(
  35. message.chat.id,
  36. text="__**Analysing Stats, Might Take 5-10 Minutes.**__",
  37. disable_web_page_preview=True
  38. )
  39. # For bot served chat and users count
  40. served_chats = []
  41. total_users = 0
  42. chats = await get_served_chats()
  43. for chat in chats:
  44. served_chats.append(int(chat["chat_id"]))
  45. for served_chat in served_chats:
  46. try:
  47. await app.get_chat_members(served_chat, BOT_ID)
  48. await asyncio.sleep(2)
  49. except Exception as e:
  50. print(f"{e} - {served_chat} in global_stats.py")
  51. await remove_served_chat(served_chat)
  52. served_chats.remove(served_chat)
  53. pass
  54. for i in served_chats:
  55. mc = (await app.get_chat(i)).members_count
  56. total_users += int(mc)
  57. # Gbans count
  58. gbans = await get_gbans_count()
  59. # Notes count across chats
  60. _notes = await get_notes_count()
  61. notes_count = _notes["notes_count"]
  62. notes_chats_count = _notes["chats_count"]
  63. # Filters count across chats
  64. _filters = await get_filters_count()
  65. filters_count = _filters["filters_count"]
  66. filters_chats_count = _filters["chats_count"]
  67. # Warns count across chats
  68. _warns = await get_warns_count()
  69. warns_count = _warns["warns_count"]
  70. warns_chats_count = _warns["chats_count"]
  71. # Karmas count across chats
  72. _karmas = await get_karmas_count()
  73. karmas_count = _karmas["karmas_count"]
  74. karmas_chats_count = _karmas["chats_count"]
  75. # Contributors/Developers count and commits on github
  76. url = "https://api.github.com/repos/thehamkercat/williambutcherbot/contributors"
  77. rurl = "https://github.com/thehamkercat/williambutcherbot"
  78. developers = await fetch(url)
  79. commits = 0
  80. for developer in developers:
  81. commits += developer['contributions']
  82. developers = len(developers)
  83. # Modules info
  84. modules_count = len(ALL_MODULES)
  85. msg = f"""
  86. **Global Stats of {BOT_NAME}**:
  87. **{modules_count}** Modules Loaded
  88. **{gbans}** Globally banned users.
  89. **{filters_count}** Filters, Across **{filters_chats_count}** chats.
  90. **{notes_count}** Notes, Across **{notes_chats_count}** chats.
  91. **{warns_count}** Warns, Across **{warns_chats_count}** chats.
  92. **{karmas_count}** Karma, Across **{karmas_chats_count}** chats.
  93. **{total_users}** Users, Across **{len(served_chats)}** chats.
  94. **{developers}** Developers And **{commits}** Commits On **[Github]({rurl})**."""
  95. await m.edit(msg, disable_web_page_preview=True)