chatbot.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 asyncio import gather, sleep
  21. from pyrogram import enums, filters
  22. from pyrogram.types import Message
  23. from wbb import (
  24. BOT_ID,
  25. SUDOERS,
  26. USERBOT_ID,
  27. USERBOT_PREFIX,
  28. USERBOT_USERNAME,
  29. app,
  30. app2,
  31. arq,
  32. eor,
  33. )
  34. from wbb.core.decorators.errors import capture_err
  35. from wbb.utils.dbfunctions import add_chatbot, check_chatbot, rm_chatbot
  36. from wbb.utils.filter_groups import chatbot_group
  37. __MODULE__ = "聊天机器人"
  38. __HELP__ = """
  39. /chatbot [开启|关闭] - 开启或关闭本群聊天机器人。
  40. 也兼容 enable 和 disable 参数。"""
  41. async def chat_bot_toggle(message: Message, is_userbot: bool):
  42. status = message.text.split(None, 1)[1].lower()
  43. status = {
  44. "开启": "enable",
  45. "打开": "enable",
  46. "关闭": "disable",
  47. }.get(status, status)
  48. chat_id = message.chat.id
  49. db = await check_chatbot()
  50. db = db["userbot"] if is_userbot else db["bot"]
  51. if status == "enable":
  52. if chat_id not in db:
  53. await add_chatbot(chat_id, is_userbot=is_userbot)
  54. text = "聊天机器人已开启。"
  55. return await eor(message, text=text)
  56. await eor(message, text="聊天机器人已经处于开启状态。")
  57. elif status == "disable":
  58. if chat_id in db:
  59. await rm_chatbot(chat_id, is_userbot=is_userbot)
  60. return await eor(message, text="聊天机器人已关闭。")
  61. await eor(message, text="聊天机器人已经处于关闭状态。")
  62. else:
  63. await eor(message, text="**用法:**\n/chatbot [开启|关闭]")
  64. # Enabled | Disable Chatbot
  65. @app.on_message(filters.command("chatbot"))
  66. @capture_err
  67. async def chatbot_status(_, message: Message):
  68. if len(message.command) != 2:
  69. return await eor(message, text="**用法:**\n/chatbot [开启|关闭]")
  70. await chat_bot_toggle(message, is_userbot=False)
  71. async def lunaQuery(query: str, user_id: int):
  72. luna = await arq.luna(query, user_id)
  73. return luna.result
  74. async def type_and_send(message: Message):
  75. chat_id = message.chat.id
  76. user_id = message.from_user.id if message.from_user else 0
  77. query = message.text.strip()
  78. await message._client.send_chat_action(chat_id, enums.ChatAction.TYPING)
  79. response, _ = await gather(lunaQuery(query, user_id), sleep(3))
  80. await message.reply_text(response)
  81. await message._client.send_chat_action(chat_id, enums.ChatAction.CANCEL)
  82. @app.on_message(
  83. filters.text
  84. & filters.reply
  85. & ~filters.bot
  86. & ~filters.via_bot
  87. & ~filters.forwarded,
  88. group=chatbot_group,
  89. )
  90. @capture_err
  91. async def chatbot_talk(_, message: Message):
  92. db = await check_chatbot()
  93. if message.chat.id not in db["bot"]:
  94. return
  95. if not message.reply_to_message:
  96. return
  97. if not message.reply_to_message.from_user:
  98. return
  99. if message.reply_to_message.from_user.id != BOT_ID:
  100. return
  101. await type_and_send(message)
  102. # FOR USERBOT
  103. @app2.on_message(
  104. filters.command("chatbot", prefixes=USERBOT_PREFIX)
  105. & SUDOERS
  106. & ~filters.forwarded
  107. & ~filters.via_bot
  108. )
  109. @capture_err
  110. async def chatbot_status_ubot(_, message: Message):
  111. if len(message.text.split()) != 2:
  112. return await eor(
  113. message,
  114. text=f"**用法:**\n{USERBOT_PREFIX}chatbot [开启|关闭]",
  115. )
  116. await chat_bot_toggle(message, is_userbot=True)
  117. @app2.on_message(
  118. ~filters.me & ~filters.private & filters.text,
  119. group=chatbot_group,
  120. )
  121. @capture_err
  122. async def chatbot_talk_ubot(_, message: Message):
  123. db = await check_chatbot()
  124. if message.chat.id not in db["userbot"]:
  125. return
  126. username = "@" + str(USERBOT_USERNAME)
  127. if message.reply_to_message:
  128. if not message.reply_to_message.from_user:
  129. return
  130. if (
  131. message.reply_to_message.from_user.id != USERBOT_ID
  132. and username not in message.text
  133. ):
  134. return
  135. else:
  136. if username not in message.text:
  137. return
  138. await type_and_send(message)
  139. @app2.on_message(
  140. filters.text & filters.private & ~filters.me,
  141. group=(chatbot_group + 1),
  142. )
  143. @capture_err
  144. async def chatbot_talk_ubot_pm(_, message: Message):
  145. db = await check_chatbot()
  146. if message.chat.id not in db["userbot"]:
  147. return
  148. await type_and_send(message)