sudoers.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. import os
  22. import subprocess
  23. import time
  24. import psutil
  25. from pyrogram import filters
  26. from pyrogram.errors import FloodWait
  27. from wbb import (BOT_ID, GBAN_LOG_GROUP_ID, SUDOERS, USERBOT_USERNAME, app,
  28. bot_start_time)
  29. from wbb.core.decorators.errors import capture_err
  30. from wbb.utils import formatter
  31. from wbb.utils.dbfunctions import (add_gban_user, get_served_chats,
  32. is_gbanned_user, remove_gban_user)
  33. from wbb.utils.functions import extract_user, extract_user_and_reason, restart
  34. __MODULE__ = "Sudoers"
  35. __HELP__ = """
  36. /stats - To Check System Status.
  37. /gstats - To Check Bot's Global Stats.
  38. /gban - To Ban A User Globally.
  39. /clean_db - Clean database.
  40. /broadcast - To Broadcast A Message To All Groups.
  41. /update - To Update And Restart The Bot
  42. /eval - Execute Python Code
  43. /sh - Execute Shell Code
  44. """
  45. # Stats Module
  46. async def bot_sys_stats():
  47. bot_uptime = int(time.time() - bot_start_time)
  48. cpu = psutil.cpu_percent()
  49. mem = psutil.virtual_memory().percent
  50. disk = psutil.disk_usage("/").percent
  51. process = psutil.Process(os.getpid())
  52. stats = f"""
  53. {USERBOT_USERNAME}@William
  54. ------------------
  55. UPTIME: {formatter.get_readable_time((bot_uptime))}
  56. BOT: {round(process.memory_info()[0] / 1024 ** 2)} MB
  57. CPU: {cpu}%
  58. RAM: {mem}%
  59. DISK: {disk}%
  60. """
  61. return stats
  62. # Gban
  63. @app.on_message(filters.command("gban") & filters.user(SUDOERS))
  64. @capture_err
  65. async def ban_globally(_, message):
  66. user_id, reason = await extract_user_and_reason(message)
  67. user = await app.get_users(user_id)
  68. from_user = message.from_user
  69. if not user_id:
  70. return await message.reply_text("I can't find that user.")
  71. if not reason:
  72. return await message.reply("No reason provided.")
  73. if user_id in ([from_user.id, BOT_ID] + SUDOERS):
  74. return await message.reply_text("No")
  75. served_chats = await get_served_chats()
  76. m = await message.reply_text(
  77. f"**Banning {user.mention} Globally!**"
  78. + f" **This Action Should Take About {len(served_chats)} Seconds.**"
  79. )
  80. await add_gban_user(user_id)
  81. number_of_chats = 0
  82. for served_chat in served_chats:
  83. try:
  84. await app.kick_chat_member(served_chat["chat_id"], user.id)
  85. number_of_chats += 1
  86. await asyncio.sleep(1)
  87. except FloodWait as e:
  88. await asyncio.sleep(int(e.x))
  89. except Exception:
  90. pass
  91. try:
  92. await app.send_message(
  93. user.id,
  94. f"Hello, You have been globally banned by {from_user.mention},"
  95. + " You can appeal for this ban by talking to him.",
  96. )
  97. except Exception:
  98. pass
  99. await m.edit(f"Banned {user.mention} Globally!")
  100. ban_text = f"""
  101. __**New Global Ban**__
  102. **Origin:** {message.chat.title} [`{message.chat.id}`]
  103. **Admin:** {from_user.mention}
  104. **Banned User:** {user.mention}
  105. **Banned User ID:** `{user_id}`
  106. **Reason:** __{reason}__
  107. **Chats:** `{number_of_chats}`"""
  108. try:
  109. m2 = await app.send_message(
  110. GBAN_LOG_GROUP_ID,
  111. text=ban_text,
  112. disable_web_page_preview=True,
  113. )
  114. await m.edit(
  115. f"Banned {user.mention} Globally!\nAction Log: {m2.link}",
  116. disable_web_page_preview=True,
  117. )
  118. except Exception:
  119. await message.reply_text(
  120. "User Gbanned, But This Gban Action Wasn't Logged, Add Me Bot In GBAN_LOG_GROUP"
  121. )
  122. # Ungban
  123. @app.on_message(filters.command("ungban") & filters.user(SUDOERS))
  124. @capture_err
  125. async def unban_globally(_, message):
  126. user_id = await extract_user(message)
  127. if not user_id:
  128. return await message.reply_text("I can't find that user.")
  129. user = await app.get_users(user_id)
  130. is_gbanned = await is_gbanned_user(user.id)
  131. if not is_gbanned:
  132. await message.reply_text("I don't remember Gbanning him.")
  133. else:
  134. await remove_gban_user(user.id)
  135. await message.reply_text(f"Lifted {user.mention}'s Global Ban.'")
  136. # Broadcast
  137. @app.on_message(
  138. filters.command("broadcast") & filters.user(SUDOERS) & ~filters.edited
  139. )
  140. @capture_err
  141. async def broadcast_message(_, message):
  142. if len(message.command) < 2:
  143. return await message.reply_text("**Usage**:\n/broadcast [MESSAGE]")
  144. sleep_time = 0.1
  145. text = message.text.split(None, 1)[1]
  146. sent = 0
  147. schats = await get_served_chats()
  148. chats = [int(chat["chat_id"]) for chat in schats]
  149. m = await message.reply_text(
  150. f"Broadcast in progress, will take {len(chats) * sleep_time} seconds."
  151. )
  152. for i in chats:
  153. try:
  154. await app.send_message(i, text=text)
  155. await asyncio.sleep(sleep_time)
  156. sent += 1
  157. except FloodWait as e:
  158. await asyncio.sleep(int(e.x))
  159. except Exception:
  160. pass
  161. await m.edit(f"**Broadcasted Message In {sent} Chats.**")
  162. # Update
  163. @app.on_message(filters.command("update") & filters.user(SUDOERS))
  164. async def update_restart(_, message):
  165. try:
  166. out = subprocess.check_output(["git", "pull"]).decode("UTF-8")
  167. if "Already up to date." in str(out):
  168. return await message.reply_text("Its already up-to date!")
  169. await message.reply_text(f"```{out}```")
  170. except Exception as e:
  171. return await message.reply_text(str(e))
  172. m = await message.reply_text(
  173. "**Updated with default branch, restarting now.**")
  174. await restart(m)