sudoers.py 11 KB

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