sudo.py 4.0 KB

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