sudoers.py 6.3 KB

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