global_stats.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """
  2. MIT License
  3. Copyright (c) 2021 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import asyncio
  21. from pyrogram import filters
  22. from pyrogram.errors import FloodWait
  23. from wbb import BOT_ID, BOT_NAME, SUDOERS, app
  24. from wbb.core.decorators.errors import capture_err
  25. from wbb.modules import ALL_MODULES
  26. from wbb.utils.dbfunctions import (get_blacklist_filters_count,
  27. get_filters_count, get_gbans_count,
  28. get_karmas_count, get_notes_count,
  29. get_rss_feeds_count,
  30. get_served_chats, get_served_users,
  31. get_warns_count,
  32. remove_served_chat)
  33. from wbb.utils.http import get
  34. from wbb.utils.inlinefuncs import keywords_list
  35. @app.on_message(
  36. filters.command("clean_db")
  37. & filters.user(SUDOERS)
  38. & ~filters.edited
  39. )
  40. @capture_err
  41. async def clean_db(_, message):
  42. served_chats = [
  43. int(i["chat_id"]) for i in (await get_served_chats())
  44. ]
  45. m = await message.reply(
  46. f"__**Cleaning database, Might take around {len(served_chats)*2} seconds.**__",
  47. )
  48. for served_chat in served_chats:
  49. try:
  50. await app.get_chat_members(served_chat, BOT_ID)
  51. await asyncio.sleep(2)
  52. except FloodWait as e:
  53. await asyncio.sleep(int(e.x))
  54. except Exception:
  55. await remove_served_chat(served_chat)
  56. served_chats.remove(served_chat)
  57. pass
  58. await m.edit("**Database Cleaned.**")
  59. # TODO Add shit to clean filters, notes and other things of left chats.
  60. @app.on_message(
  61. filters.command("gstats")
  62. & filters.user(SUDOERS)
  63. & ~filters.edited
  64. )
  65. @capture_err
  66. async def global_stats(_, message):
  67. m = await app.send_message(
  68. message.chat.id,
  69. text="__**Analysing Stats...**__",
  70. disable_web_page_preview=True,
  71. )
  72. # For bot served chat and users count
  73. served_chats = len(await get_served_chats())
  74. served_users = len(await get_served_users())
  75. # Gbans count
  76. gbans = await get_gbans_count()
  77. _notes = await get_notes_count()
  78. notes_count = _notes["notes_count"]
  79. notes_chats_count = _notes["chats_count"]
  80. # Filters count across chats
  81. _filters = await get_filters_count()
  82. filters_count = _filters["filters_count"]
  83. filters_chats_count = _filters["chats_count"]
  84. # Blacklisted filters count across chats
  85. _filters = await get_blacklist_filters_count()
  86. blacklist_filters_count = _filters["filters_count"]
  87. blacklist_filters_chats_count = _filters["chats_count"]
  88. # Warns count across chats
  89. _warns = await get_warns_count()
  90. warns_count = _warns["warns_count"]
  91. warns_chats_count = _warns["chats_count"]
  92. # Karmas count across chats
  93. _karmas = await get_karmas_count()
  94. karmas_count = _karmas["karmas_count"]
  95. karmas_chats_count = _karmas["chats_count"]
  96. # Contributors/Developers count and commits on github
  97. url = "https://api.github.com/repos/thehamkercat/williambutcherbot/contributors"
  98. rurl = "https://github.com/thehamkercat/williambutcherbot"
  99. developers = await get(url)
  100. commits = 0
  101. for developer in developers:
  102. commits += developer["contributions"]
  103. developers = len(developers)
  104. # Rss feeds
  105. rss_count = await get_rss_feeds_count()
  106. # Modules info
  107. modules_count = len(ALL_MODULES)
  108. msg = f"""
  109. **Global Stats of {BOT_NAME}**:
  110. **{modules_count}** Modules Loaded
  111. **{len(keywords_list)}** Inline Modules Loaded.
  112. **{rss_count}** Active RSS Feeds.
  113. **{gbans}** Globally banned users.
  114. **{filters_count}** Filters, Across **{filters_chats_count}** chats.
  115. **{blacklist_filters_count}** Blacklist Filters, Across **{blacklist_filters_chats_count}** chats.
  116. **{notes_count}** Notes, Across **{notes_chats_count}** chats.
  117. **{warns_count}** Warns, Across **{warns_chats_count}** chats.
  118. **{karmas_count}** Karma, Across **{karmas_chats_count}** chats.
  119. **{served_users}** Users, Across **{served_chats}** chats.
  120. **{developers}** Developers And **{commits}** Commits On **[Github]({rurl})**.
  121. """
  122. await m.edit(msg, disable_web_page_preview=True)