rules.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """
  2. MIT License
  3. Copyright (c) 2024 SI_NN_ER_LS
  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 pyrogram import filters
  21. from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
  22. from wbb import BOT_USERNAME, app
  23. from wbb.core.decorators.permissions import adminsOnly
  24. from wbb.core.keyboard import ikb
  25. from wbb.modules.admin import member_permissions
  26. from wbb.modules.notes import extract_urls
  27. from wbb.utils.dbfunctions import delete_rules, get_rules, set_chat_rules
  28. from wbb.utils.functions import check_format
  29. __MODULE__ = "Rules"
  30. __HELP__ = """
  31. • /rules: get the rules for this chat.
  32. **Admins only:**
  33. • /setrules: Reply to a message to set the rules for the chat.
  34. • /clearrules: clear the rules for this chat.
  35. """
  36. @app.on_message(filters.command("rules") & ~filters.private)
  37. async def send_rules(_, message):
  38. chat_id = message.chat.id
  39. replied_message = message.reply_to_message
  40. if replied_message:
  41. message = replied_message
  42. await message.reply_text(
  43. "**Click on the button to see the chat rules!**",
  44. reply_markup=InlineKeyboardMarkup(
  45. [
  46. [
  47. InlineKeyboardButton(
  48. "rules",
  49. url=f"t.me/{BOT_USERNAME}?start=rules_{chat_id}",
  50. )
  51. ]
  52. ]
  53. ),
  54. )
  55. @app.on_message(filters.command("setrules") & ~filters.private)
  56. @adminsOnly("can_change_info")
  57. async def set_rules(_, message):
  58. try:
  59. chat_id = message.chat.id
  60. replied_message = message.reply_to_message
  61. if len(message.command) < 2 and not replied_message:
  62. return await message.reply(
  63. "**Reply to a message to set new rules.**"
  64. )
  65. if len(message.command) < 2 and replied_message.text:
  66. rules = replied_message.text.markdown
  67. if replied_message.reply_markup:
  68. urls = extract_urls(replied_message.reply_markup)
  69. if urls:
  70. response = "\n".join(
  71. [f"{name}=[{text}, {url}]" for name, text, url in urls]
  72. )
  73. rules = rules + response
  74. else:
  75. text = message.text.markdown
  76. rules = text.split(" ", 1)[1]
  77. rules = await check_format(ikb, rules)
  78. if not rules:
  79. return await message.reply_text(
  80. "**Wrong formatting, check the help section.**"
  81. )
  82. await set_chat_rules(chat_id, rules)
  83. return await message.reply_text(
  84. "**Successfully set new rules for this chat.**"
  85. )
  86. except Exception:
  87. await message.reply_text(
  88. "**You can only set text messages as rules.**"
  89. )
  90. @app.on_message(filters.command("clearrules") & ~filters.private)
  91. @adminsOnly("can_change_info")
  92. async def delete_rules_cmd(_, message):
  93. rules = await get_rules(message.chat.id)
  94. if not rules:
  95. await message.reply_text("**No rules in this chat.**")
  96. else:
  97. keyboard = InlineKeyboardMarkup(
  98. [
  99. [
  100. InlineKeyboardButton(
  101. "YES, DO IT", callback_data="drules_yes"
  102. ),
  103. InlineKeyboardButton("Cancel", callback_data="drules_no"),
  104. ]
  105. ]
  106. )
  107. await message.reply_text(
  108. "**Are you sure to delete the current rules ?.**",
  109. reply_markup=keyboard,
  110. )
  111. @app.on_callback_query(filters.regex("drules_(.*)"))
  112. async def delete_rules_cb(_, cb):
  113. chat_id = cb.message.chat.id
  114. from_user = cb.from_user
  115. permissions = await member_permissions(chat_id, from_user.id)
  116. permission = "can_change_info"
  117. if permission not in permissions:
  118. return await cb.answer(
  119. f"You don't have the required permission.\n Permission: {permission}",
  120. show_alert=True,
  121. )
  122. input = cb.data.split("_", 1)[1]
  123. if input == "yes":
  124. deleted = await delete_rules(chat_id)
  125. if deleted:
  126. return await cb.message.edit(
  127. "**Successfully deleted rules of this chat.**"
  128. )
  129. if input == "no":
  130. await cb.message.reply_to_message.delete()
  131. await cb.message.delete()