permissions.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 functools import wraps
  21. from traceback import format_exc as err
  22. from pyrogram.errors.exceptions.forbidden_403 import \
  23. ChatWriteForbidden
  24. from pyrogram.types import Message
  25. from wbb import SUDOERS, app
  26. from wbb.modules.admin import member_permissions
  27. async def authorised(
  28. func, subFunc2, client, message, *args, **kwargs
  29. ):
  30. chatID = message.chat.id
  31. try:
  32. await func(client, message, *args, **kwargs)
  33. except ChatWriteForbidden:
  34. await app.leave_chat(chatID)
  35. except Exception as e:
  36. await message.reply_text(str(e.MESSAGE))
  37. e = err()
  38. print(str(e))
  39. return subFunc2
  40. async def unauthorised(message: Message, permission, subFunc2):
  41. chatID = message.chat.id
  42. text = (
  43. "You don't have the required permission to perform this action."
  44. + f"\n**Permission:** __{permission}__"
  45. )
  46. try:
  47. await message.reply_text(text)
  48. except ChatWriteForbidden:
  49. await app.leave_chat(chatID)
  50. return subFunc2
  51. def adminsOnly(permission):
  52. def subFunc(func):
  53. @wraps(func)
  54. async def subFunc2(client, message: Message, *args, **kwargs):
  55. chatID = message.chat.id
  56. if not message.from_user:
  57. # For anonymous admins
  58. if message.sender_chat:
  59. return await authorised(
  60. func,
  61. subFunc2,
  62. client,
  63. message,
  64. *args,
  65. **kwargs,
  66. )
  67. return await unauthorised(
  68. message, permission, subFunc2
  69. )
  70. # For admins and sudo users
  71. userID = message.from_user.id
  72. permissions = await member_permissions(chatID, userID)
  73. if (
  74. userID not in SUDOERS
  75. and permission not in permissions
  76. ):
  77. return await unauthorised(
  78. message, permission, subFunc2
  79. )
  80. return await authorised(
  81. func, subFunc2, client, message, *args, **kwargs
  82. )
  83. return subFunc2
  84. return subFunc