sudoers.py 11 KB

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