rules.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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__ = "群规"
  30. __HELP__ = """
  31. • /rules:查看本群群规,也可直接发送“群规”。
  32. **仅管理员:**
  33. • /setrules:回复一条消息,将其设为群规。
  34. • /clearrules:清空本群群规。
  35. """
  36. @app.on_message(
  37. (filters.command("rules") | filters.regex(r"^群规$"))
  38. & ~filters.private
  39. )
  40. async def send_rules(_, message):
  41. chat_id = message.chat.id
  42. replied_message = message.reply_to_message
  43. if replied_message:
  44. message = replied_message
  45. await message.reply_text(
  46. "**点击下方按钮查看群规。**",
  47. reply_markup=InlineKeyboardMarkup(
  48. [
  49. [
  50. InlineKeyboardButton(
  51. "查看群规",
  52. url=f"t.me/{BOT_USERNAME}?start=rules_{chat_id}",
  53. )
  54. ]
  55. ]
  56. ),
  57. )
  58. @app.on_message(filters.command("setrules") & ~filters.private)
  59. @adminsOnly("can_change_info")
  60. async def set_rules(_, message):
  61. try:
  62. chat_id = message.chat.id
  63. replied_message = message.reply_to_message
  64. if len(message.command) < 2 and not replied_message:
  65. return await message.reply(
  66. "**请回复一条消息,将其设为新群规。**"
  67. )
  68. if len(message.command) < 2 and replied_message.text:
  69. rules = replied_message.text.markdown
  70. if replied_message.reply_markup:
  71. urls = extract_urls(replied_message.reply_markup)
  72. if urls:
  73. response = "\n".join(
  74. [f"{name}=[{text}, {url}]" for name, text, url in urls]
  75. )
  76. rules = rules + response
  77. else:
  78. text = message.text.markdown
  79. rules = text.split(" ", 1)[1]
  80. rules = await check_format(ikb, rules)
  81. if not rules:
  82. return await message.reply_text(
  83. "**消息格式不正确,请发送 /markdownhelp 查看说明。**"
  84. )
  85. await set_chat_rules(chat_id, rules)
  86. return await message.reply_text(
  87. "**本群群规已更新。**"
  88. )
  89. except Exception:
  90. await message.reply_text(
  91. "**群规只能使用文字消息。**"
  92. )
  93. @app.on_message(filters.command("clearrules") & ~filters.private)
  94. @adminsOnly("can_change_info")
  95. async def delete_rules_cmd(_, message):
  96. rules = await get_rules(message.chat.id)
  97. if not rules:
  98. await message.reply_text("**本群尚未设置群规。**")
  99. else:
  100. keyboard = InlineKeyboardMarkup(
  101. [
  102. [
  103. InlineKeyboardButton(
  104. "确认删除", callback_data="drules_yes"
  105. ),
  106. InlineKeyboardButton("取消", callback_data="drules_no"),
  107. ]
  108. ]
  109. )
  110. await message.reply_text(
  111. "**确认删除当前群规吗?**",
  112. reply_markup=keyboard,
  113. )
  114. @app.on_callback_query(filters.regex("drules_(.*)"))
  115. async def delete_rules_cb(_, cb):
  116. chat_id = cb.message.chat.id
  117. from_user = cb.from_user
  118. permissions = await member_permissions(chat_id, from_user.id)
  119. permission = "can_change_info"
  120. if permission not in permissions:
  121. return await cb.answer(
  122. "你没有修改群信息的权限。",
  123. show_alert=True,
  124. )
  125. input = cb.data.split("_", 1)[1]
  126. if input == "yes":
  127. deleted = await delete_rules(chat_id)
  128. if deleted:
  129. return await cb.message.edit(
  130. "**本群群规已删除。**"
  131. )
  132. if input == "no":
  133. await cb.message.reply_to_message.delete()
  134. await cb.message.delete()