global_stats.py 3.3 KB

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