sudoers.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. start_restart_stage)
  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. /broadcast - To Broadcast A Message To All Groups.
  40. /update - To Update And Restart The Bot
  41. """
  42. # Stats Module
  43. async def bot_sys_stats():
  44. bot_uptime = int(time.time() - bot_start_time)
  45. cpu = psutil.cpu_percent()
  46. mem = psutil.virtual_memory().percent
  47. disk = psutil.disk_usage("/").percent
  48. process = psutil.Process(os.getpid())
  49. stats = f"""
  50. {USERBOT_USERNAME}@William
  51. ------------------
  52. UPTIME: {formatter.get_readable_time((bot_uptime))}
  53. BOT: {round(process.memory_info()[0] / 1024 ** 2)} MB
  54. CPU: {cpu}%
  55. RAM: {mem}%
  56. DISK: {disk}%
  57. """
  58. return stats
  59. @app.on_message(filters.user(SUDOERS) & filters.command("stats"))
  60. @capture_err
  61. async def get_stats(_, message):
  62. stats = await bot_sys_stats()
  63. await message.reply_text(stats)
  64. # Gban
  65. @app.on_message(filters.command("gban") & filters.user(SUDOERS))
  66. @capture_err
  67. async def ban_globally(_, message):
  68. if not message.reply_to_message:
  69. if len(message.command) < 3:
  70. return await message.reply_text(
  71. "**Usage:**\n/gban [USERNAME | USER_ID] [REASON]"
  72. )
  73. user = message.text.split(None, 2)[1]
  74. reason = message.text.split(None, 2)[2]
  75. if "@" in user:
  76. user = user.replace("@", "")
  77. user = await app.get_users(user)
  78. from_user = message.from_user
  79. if user.id == from_user.id:
  80. await message.reply_text("You want to gban yourself? FOOL!")
  81. elif user.id == BOT_ID:
  82. await message.reply_text(
  83. "Should i gban myself? I'm not fool like you, HUMAN!"
  84. )
  85. elif user.id in SUDOERS:
  86. await message.reply_text("You want to ban a sudo user? GET REKT!!")
  87. else:
  88. served_chats = await get_served_chats()
  89. m = await message.reply_text(
  90. f"**Initializing WBB Global Ban Sequence To Add Restrictions On {user.mention}**"
  91. + f" **This Action Should Take About {len(served_chats)} Seconds.**"
  92. )
  93. await add_gban_user(user.id)
  94. number_of_chats = 0
  95. for served_chat in served_chats:
  96. try:
  97. await app.kick_chat_member(served_chat["chat_id"], user.id)
  98. number_of_chats += 1
  99. await asyncio.sleep(1)
  100. except Exception:
  101. pass
  102. try:
  103. await app.send_message(
  104. user.id,
  105. f"Hello, You have been globally banned by {from_user.mention},"
  106. + " You can appeal for this ban in @WBBSupport.",
  107. )
  108. except Exception:
  109. pass
  110. await m.edit(f"Banned {user.mention} Globally!")
  111. ban_text = f"""
  112. __**New Global Ban**__
  113. **Origin:** {message.chat.title} [`{message.chat.id}`]
  114. **Admin:** {from_user.mention}
  115. **Banned User:** {user.mention}
  116. **Banned User ID:** `{user.id}`
  117. **Reason:** __{reason}__
  118. **Chats:** `{number_of_chats}`"""
  119. try:
  120. m2 = await app.send_message(
  121. GBAN_LOG_GROUP_ID,
  122. text=ban_text,
  123. disable_web_page_preview=True,
  124. )
  125. await m.edit(
  126. f"Banned {user.mention} Globally!\nAction Log: {m2.link}",
  127. disable_web_page_preview=True,
  128. )
  129. except Exception:
  130. await message.reply_text(
  131. "User Gbanned, But This Gban Wasn't Logged, Add Bot In GBAN_LOG_GROUP"
  132. )
  133. return
  134. return
  135. if len(message.command) < 2:
  136. await message.reply_text("**Usage:**\n/gban [REASON]")
  137. return
  138. reason = message.text.strip().split(None, 1)[1]
  139. from_user_id = message.from_user.id
  140. from_user_mention = message.from_user.mention
  141. user_id = message.reply_to_message.from_user.id
  142. mention = message.reply_to_message.from_user.mention
  143. if user_id == from_user_id:
  144. await message.reply_text("You want to gban yourself? FOOL!")
  145. elif user_id == BOT_ID:
  146. await message.reply_text(
  147. "Should i gban myself? I'm not fool like you, HUMAN!"
  148. )
  149. elif user_id in SUDOERS:
  150. await message.reply_text("You want to ban a sudo user? GET REKT!!")
  151. else:
  152. is_gbanned = await is_gbanned_user(user_id)
  153. if is_gbanned:
  154. await message.reply_text("He's already gbanned, why bully him?")
  155. else:
  156. served_chats = await get_served_chats()
  157. m = await message.reply_text(
  158. f"**Initializing WBB Global Ban Sequence To Add Restrictions On {mention}**"
  159. + f" **This Action Should Take About {len(served_chats)} Seconds.**"
  160. )
  161. number_of_chats = 0
  162. for served_chat in served_chats:
  163. try:
  164. await app.kick_chat_member(served_chat["chat_id"], user_id)
  165. number_of_chats += 1
  166. await asyncio.sleep(1)
  167. except Exception:
  168. pass
  169. await add_gban_user(user_id)
  170. try:
  171. await app.send_message(
  172. user_id,
  173. f"""
  174. Hello, You have been globally banned by {from_user_mention},
  175. You can appeal for this ban in @WBBSupport.""",
  176. )
  177. except Exception:
  178. pass
  179. await m.edit(f"Banned {mention} Globally!")
  180. ban_text = f"""
  181. __**New Global Ban**__
  182. **Origin:** {message.chat.title} [`{message.chat.id}`]
  183. **Admin:** {from_user_mention}
  184. **Banned User:** {mention}
  185. **Banned User ID:** `{user_id}`
  186. **Reason:** __{reason}__
  187. **Chats:** `{number_of_chats}`"""
  188. try:
  189. m2 = await app.send_message(
  190. GBAN_LOG_GROUP_ID,
  191. text=ban_text,
  192. disable_web_page_preview=True,
  193. )
  194. await m.edit(
  195. f"Banned {mention} Globally!\nAction Log: {m2.link}",
  196. disable_web_page_preview=True,
  197. )
  198. except Exception:
  199. await message.reply_text(
  200. "User Gbanned, But This Gban Wasn't Logged, Add Bot In GBAN_LOG_GROUP"
  201. )
  202. # Ungban
  203. @app.on_message(filters.command("ungban") & filters.user(SUDOERS))
  204. @capture_err
  205. async def unban_globally(_, message):
  206. if not message.reply_to_message:
  207. if len(message.command) != 2:
  208. return await message.reply_text(
  209. "Reply to a user's message or give username/user_id."
  210. )
  211. user = message.text.split(None, 1)[1]
  212. if "@" in user:
  213. user = user.replace("@", "")
  214. user = await app.get_users(user)
  215. from_user = message.from_user
  216. if user.id == from_user.id:
  217. await message.reply_text("You want to ungban yourself? FOOL!")
  218. elif user.id == BOT_ID:
  219. await message.reply_text(
  220. "Should i ungban myself? But i'm not gbanned."
  221. )
  222. elif user.id in SUDOERS:
  223. await message.reply_text("Sudo users can't be gbanned/ungbanned.")
  224. else:
  225. is_gbanned = await is_gbanned_user(user.id)
  226. if not is_gbanned:
  227. await message.reply_text("He's already free, why bully him?")
  228. else:
  229. await remove_gban_user(user.id)
  230. await message.reply_text(f"Unbanned {user.mention} Globally!")
  231. return
  232. from_user_id = message.from_user.id
  233. user_id = message.reply_to_message.from_user.id
  234. mention = message.reply_to_message.from_user.mention
  235. if user_id == from_user_id:
  236. await message.reply_text("You want to ungban yourself? FOOL!")
  237. elif user_id == BOT_ID:
  238. await message.reply_text(
  239. "Should i ungban myself? But i'm not gbanned."
  240. )
  241. elif user_id in SUDOERS:
  242. await message.reply_text("Sudo users can't be gbanned/ungbanned.")
  243. else:
  244. is_gbanned = await is_gbanned_user(user_id)
  245. if not is_gbanned:
  246. await message.reply_text("He's already free, why bully him?")
  247. else:
  248. await remove_gban_user(user_id)
  249. await message.reply_text(f"Unbanned {mention} Globally!")
  250. # Broadcast
  251. @app.on_message(
  252. filters.command("broadcast") & filters.user(SUDOERS) & ~filters.edited
  253. )
  254. @capture_err
  255. async def broadcast_message(_, message):
  256. if len(message.command) < 2:
  257. return await message.reply_text("**Usage**:\n/broadcast [MESSAGE]")
  258. sleep_time = 3
  259. text = message.text.split(None, 1)[1]
  260. sent = 0
  261. schats = await get_served_chats()
  262. chats = [int(chat["chat_id"]) for chat in schats]
  263. m = await message.reply_text(
  264. f"Broadcast in progress, will take {len(chats) * sleep_time} seconds."
  265. )
  266. for i in chats:
  267. try:
  268. await app.send_message(i, text=text)
  269. await asyncio.sleep(sleep_time)
  270. sent += 1
  271. except FloodWait as e:
  272. await asyncio.sleep(int(e.x))
  273. except Exception:
  274. pass
  275. await m.edit(f"**Broadcasted Message In {sent} Chats.**")
  276. # Update
  277. @app.on_message(filters.command("update") & filters.user(SUDOERS))
  278. async def update_restart(_, message):
  279. await message.reply_text(
  280. f'```{subprocess.check_output(["git", "pull"]).decode("UTF-8")}```'
  281. )
  282. m = await message.reply_text(
  283. "**Updated with default branch, restarting now**"
  284. )
  285. await start_restart_stage(m.chat.id, m.message_id)
  286. os.execvp("python3", ["python3", "-m", "wbb"])