misc.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 math import ceil
  21. from pyrogram.types import InlineKeyboardButton
  22. from wbb import MOD_LOAD, MOD_NOLOAD
  23. class EqInlineKeyboardButton(InlineKeyboardButton):
  24. def __eq__(self, other):
  25. return self.text == other.text
  26. def __lt__(self, other):
  27. return self.text < other.text
  28. def __gt__(self, other):
  29. return self.text > other.text
  30. def paginate_modules(page_n, module_dict, prefix, chat=None):
  31. if not chat:
  32. modules = sorted(
  33. [
  34. EqInlineKeyboardButton(
  35. x.__MODULE__,
  36. callback_data="{}_module({})".format(
  37. prefix, x.__MODULE__.replace(" ", "_").lower()
  38. ),
  39. )
  40. for x in module_dict.values()
  41. ]
  42. )
  43. else:
  44. modules = sorted(
  45. [
  46. EqInlineKeyboardButton(
  47. x.__MODULE__,
  48. callback_data="{}_module({},{})".format(
  49. prefix, chat, x.__MODULE__.replace(" ", "_").lower()
  50. ),
  51. )
  52. for x in module_dict.values()
  53. ]
  54. )
  55. pairs = list(zip(modules[::3], modules[1::3], modules[2::3]))
  56. i = 0
  57. for m in pairs:
  58. for _ in m:
  59. i += 1
  60. if len(modules) - i == 1:
  61. pairs.append((modules[-1],))
  62. elif len(modules) - i == 2:
  63. pairs.append(
  64. (
  65. modules[-2],
  66. modules[-1],
  67. )
  68. )
  69. COLUMN_SIZE = 4
  70. max_num_pages = ceil(len(pairs) / COLUMN_SIZE)
  71. modulo_page = page_n % max_num_pages
  72. # can only have a certain amount of buttons side by side
  73. if len(pairs) > COLUMN_SIZE:
  74. pairs = pairs[
  75. modulo_page * COLUMN_SIZE : COLUMN_SIZE * (modulo_page + 1)
  76. ] + [
  77. (
  78. EqInlineKeyboardButton(
  79. "❮",
  80. callback_data="{}_prev({})".format(prefix, modulo_page),
  81. ),
  82. EqInlineKeyboardButton(
  83. "Back",
  84. callback_data="{}_home({})".format(prefix, modulo_page),
  85. ),
  86. EqInlineKeyboardButton(
  87. "❯",
  88. callback_data="{}_next({})".format(prefix, modulo_page),
  89. ),
  90. )
  91. ]
  92. else:
  93. pairs = pairs[
  94. modulo_page * COLUMN_SIZE : COLUMN_SIZE * (modulo_page + 1)
  95. ] + [
  96. (
  97. EqInlineKeyboardButton(
  98. "Back",
  99. callback_data="{}_home({})".format(prefix, modulo_page),
  100. ),
  101. )
  102. ]
  103. return pairs
  104. def is_module_loaded(name):
  105. return (not MOD_LOAD or name in MOD_LOAD) and name not in MOD_NOLOAD