permissions.py 3.3 KB

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