global_stats.py 3.5 KB

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