sudoers.py 10 KB

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