sudoers.py 9.8 KB

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