sudoers.py 11 KB

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