misc.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from math import ceil
  2. from pyrogram.types import InlineKeyboardButton
  3. from wbb import MOD_LOAD, MOD_NOLOAD
  4. class EqInlineKeyboardButton(InlineKeyboardButton):
  5. def __eq__(self, other):
  6. return self.text == other.text
  7. def __lt__(self, other):
  8. return self.text < other.text
  9. def __gt__(self, other):
  10. return self.text > other.text
  11. def paginate_modules(page_n, module_dict, prefix, chat=None):
  12. if not chat:
  13. modules = sorted(
  14. [
  15. EqInlineKeyboardButton(
  16. x.__MODULE__,
  17. callback_data="{}_module({})".format(
  18. prefix, x.__MODULE__.lower()
  19. ),
  20. )
  21. for x in module_dict.values()
  22. ]
  23. )
  24. else:
  25. modules = sorted(
  26. [
  27. EqInlineKeyboardButton(
  28. x.__MODULE__,
  29. callback_data="{}_module({},{})".format(
  30. prefix, chat, x.__MODULE__.lower()
  31. ),
  32. )
  33. for x in module_dict.values()
  34. ]
  35. )
  36. pairs = list(zip(modules[::3], modules[1::3], modules[2::3]))
  37. c = 0
  38. for x in pairs:
  39. for _ in x:
  40. c += 1
  41. if len(modules) - c == 1:
  42. pairs.append((modules[-1],))
  43. elif len(modules) - c == 2:
  44. pairs.append(
  45. (
  46. modules[-2],
  47. modules[-1],
  48. )
  49. )
  50. max_num_pages = ceil(len(pairs) / 7)
  51. modulo_page = page_n % max_num_pages
  52. # can only have a certain amount of buttons side by side
  53. if len(pairs) > 7:
  54. pairs = pairs[modulo_page * 7: 7 * (modulo_page + 1)] + [
  55. (
  56. EqInlineKeyboardButton(
  57. "<",
  58. callback_data="{}_prev({})".format(
  59. prefix, modulo_page
  60. ),
  61. ),
  62. EqInlineKeyboardButton(
  63. ">",
  64. callback_data="{}_next({})".format(
  65. prefix, modulo_page
  66. ),
  67. ),
  68. )
  69. ]
  70. return pairs
  71. def is_module_loaded(name):
  72. return (not MOD_LOAD or name in MOD_LOAD) and name not in MOD_NOLOAD