misc.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. for x in module_dict.values()
  21. ]
  22. )
  23. else:
  24. modules = sorted(
  25. [
  26. EqInlineKeyboardButton(
  27. x.__MODULE__,
  28. callback_data="{}_module({},{})".format(
  29. prefix, chat, x.__MODULE__.lower()
  30. ),
  31. )
  32. for x in module_dict.values()
  33. ]
  34. )
  35. pairs = list(zip(modules[::3], modules[1::3], modules[2::3]))
  36. c = 0
  37. for x in pairs:
  38. for _ in x:
  39. c += 1
  40. if len(modules) - c == 1:
  41. pairs.append((modules[-1],))
  42. elif len(modules) - c == 2:
  43. pairs.append(
  44. (
  45. modules[-2],
  46. modules[-1],
  47. )
  48. )
  49. max_num_pages = ceil(len(pairs) / 7)
  50. modulo_page = page_n % max_num_pages
  51. # can only have a certain amount of buttons side by side
  52. if len(pairs) > 7:
  53. pairs = pairs[modulo_page * 7: 7 * (modulo_page + 1)] + [
  54. (
  55. EqInlineKeyboardButton(
  56. "<",
  57. callback_data="{}_prev({})".format(
  58. prefix, modulo_page
  59. ),
  60. ),
  61. EqInlineKeyboardButton(
  62. ">",
  63. callback_data="{}_next({})".format(
  64. prefix, modulo_page
  65. ),
  66. ),
  67. )
  68. ]
  69. return pairs
  70. def is_module_loaded(name):
  71. return (not MOD_LOAD or name in MOD_LOAD) and name not in MOD_NOLOAD