sudoers.py 7.9 KB

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