permissions.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from functools import wraps
  2. from pyrogram.errors.exceptions.forbidden_403 import ChatWriteForbidden
  3. from pyrogram.types import Message
  4. from wbb import SUDOERS, app
  5. from wbb.modules.admin import member_permissions
  6. async def authorised(func, subFunc2, client, message, *args, **kwargs):
  7. chatID = message.chat.id
  8. try:
  9. await func(client, message, *args, **kwargs)
  10. except ChatWriteForbidden:
  11. await app.leave_chat(chatID)
  12. except Exception as e:
  13. try:
  14. await message.reply_text(str(e))
  15. except ChatWriteForbidden:
  16. await app.leave_chat(chatID)
  17. return subFunc2
  18. async def unauthorised(message: Message, permission, subFunc2):
  19. chatID = message.chat.id
  20. text = (
  21. "You don't have the required permission to perform this action."
  22. + f"\n**Permission:** __{permission}__"
  23. )
  24. try:
  25. await message.reply_text(text)
  26. except ChatWriteForbidden:
  27. await app.leave_chat(chatID)
  28. return subFunc2
  29. def adminsOnly(permission):
  30. def subFunc(func):
  31. @wraps(func)
  32. async def subFunc2(client, message: Message, *args, **kwargs):
  33. chatID = message.chat.id
  34. if not message.from_user:
  35. # For anonymous admins
  36. if message.sender_chat:
  37. return await authorised(
  38. func, subFunc2, client, message, *args, **kwargs
  39. )
  40. return await unauthorised(message, permission, subFunc2)
  41. # For admins and sudo users
  42. userID = message.from_user.id
  43. permissions = await member_permissions(chatID, userID)
  44. if userID not in SUDOERS and permission not in permissions:
  45. return await unauthorised(message, permission, subFunc2)
  46. return await authorised(
  47. func, subFunc2, client, message, *args, **kwargs
  48. )
  49. return subFunc2
  50. return subFunc