sudoers.py 8.0 KB

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