sudo.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """
  2. MIT License
  3. Copyright (c) 2024 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. from pyrogram import filters
  21. from pyrogram.types import Message
  22. from wbb import BOT_ID, SUDOERS, USERBOT_PREFIX, app2, eor
  23. from wbb.core.decorators.errors import capture_err
  24. from wbb.utils.dbfunctions import add_sudo, get_sudoers, remove_sudo
  25. __MODULE__ = "Sudo"
  26. __HELP__ = """
  27. **THIS MODULE IS ONLY FOR DEVS**
  28. .useradd - To Add A User In Sudoers.
  29. .userdel - To Remove A User From Sudoers.
  30. .sudoers - To List Sudo Users.
  31. **NOTE:**
  32. Never add anyone to sudoers unless you trust them,
  33. sudo users can do anything with your account, they
  34. can even delete your account.
  35. """
  36. @app2.on_message(
  37. filters.command("useradd", prefixes=USERBOT_PREFIX)
  38. & ~filters.forwarded
  39. & ~filters.via_bot
  40. & SUDOERS
  41. )
  42. @capture_err
  43. async def useradd(_, message: Message):
  44. if not message.reply_to_message:
  45. return await eor(
  46. message,
  47. text="Reply to someone's message to add him to sudoers.",
  48. )
  49. user_id = message.reply_to_message.from_user.id
  50. umention = (await app2.get_users(user_id)).mention
  51. sudoers = await get_sudoers()
  52. if user_id in sudoers:
  53. return await eor(message, text=f"{umention} is already in sudoers.")
  54. if user_id == BOT_ID:
  55. return await eor(
  56. message, text="You can't add assistant bot in sudoers."
  57. )
  58. await add_sudo(user_id)
  59. if user_id not in SUDOERS:
  60. SUDOERS.add(user_id)
  61. await eor(
  62. message,
  63. text=f"Successfully added {umention} in sudoers.",
  64. )
  65. @app2.on_message(
  66. filters.command("userdel", prefixes=USERBOT_PREFIX)
  67. & ~filters.forwarded
  68. & ~filters.via_bot
  69. & SUDOERS
  70. )
  71. @capture_err
  72. async def userdel(_, message: Message):
  73. if not message.reply_to_message:
  74. return await eor(
  75. message,
  76. text="Reply to someone's message to remove him to sudoers.",
  77. )
  78. user_id = message.reply_to_message.from_user.id
  79. umention = (await app2.get_users(user_id)).mention
  80. if user_id not in await get_sudoers():
  81. return await eor(message, text=f"{umention} is not in sudoers.")
  82. await remove_sudo(user_id)
  83. if user_id in SUDOERS:
  84. SUDOERS.remove(user_id)
  85. await eor(
  86. message,
  87. text=f"Successfully removed {umention} from sudoers.",
  88. )
  89. @app2.on_message(
  90. filters.command("sudoers", prefixes=USERBOT_PREFIX)
  91. & ~filters.forwarded
  92. & ~filters.via_bot
  93. & SUDOERS
  94. )
  95. @capture_err
  96. async def sudoers_list(_, message: Message):
  97. sudoers = await get_sudoers()
  98. text = ""
  99. j = 0
  100. for user_id in sudoers:
  101. try:
  102. user = await app2.get_users(user_id)
  103. user = user.first_name if not user.mention else user.mention
  104. j += 1
  105. except Exception:
  106. continue
  107. text += f"{j}. {user}\n"
  108. if text == "":
  109. return await eor(message, text="No sudoers found.")
  110. await eor(message, text=text)