global_stats.py 3.3 KB

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