sudoers.py 11 KB

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