| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- """
- MIT License
- Copyright (c) 2024 TheHamkerCat
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- """
- from asyncio import gather, sleep
- from pyrogram import enums, filters
- from pyrogram.types import Message
- from wbb import (
- BOT_ID,
- SUDOERS,
- USERBOT_ID,
- USERBOT_PREFIX,
- USERBOT_USERNAME,
- app,
- app2,
- arq,
- eor,
- )
- from wbb.core.decorators.errors import capture_err
- from wbb.utils.dbfunctions import add_chatbot, check_chatbot, rm_chatbot
- from wbb.utils.filter_groups import chatbot_group
- __MODULE__ = "聊天机器人"
- __HELP__ = """
- /chatbot [开启|关闭] - 开启或关闭本群聊天机器人。
- 也兼容 enable 和 disable 参数。"""
- async def chat_bot_toggle(message: Message, is_userbot: bool):
- status = message.text.split(None, 1)[1].lower()
- status = {
- "开启": "enable",
- "打开": "enable",
- "关闭": "disable",
- }.get(status, status)
- chat_id = message.chat.id
- db = await check_chatbot()
- db = db["userbot"] if is_userbot else db["bot"]
- if status == "enable":
- if chat_id not in db:
- await add_chatbot(chat_id, is_userbot=is_userbot)
- text = "聊天机器人已开启。"
- return await eor(message, text=text)
- await eor(message, text="聊天机器人已经处于开启状态。")
- elif status == "disable":
- if chat_id in db:
- await rm_chatbot(chat_id, is_userbot=is_userbot)
- return await eor(message, text="聊天机器人已关闭。")
- await eor(message, text="聊天机器人已经处于关闭状态。")
- else:
- await eor(message, text="**用法:**\n/chatbot [开启|关闭]")
- # Enabled | Disable Chatbot
- @app.on_message(filters.command("chatbot"))
- @capture_err
- async def chatbot_status(_, message: Message):
- if len(message.command) != 2:
- return await eor(message, text="**用法:**\n/chatbot [开启|关闭]")
- await chat_bot_toggle(message, is_userbot=False)
- async def lunaQuery(query: str, user_id: int):
- luna = await arq.luna(query, user_id)
- return luna.result
- async def type_and_send(message: Message):
- chat_id = message.chat.id
- user_id = message.from_user.id if message.from_user else 0
- query = message.text.strip()
- await message._client.send_chat_action(chat_id, enums.ChatAction.TYPING)
- response, _ = await gather(lunaQuery(query, user_id), sleep(3))
- await message.reply_text(response)
- await message._client.send_chat_action(chat_id, enums.ChatAction.CANCEL)
- @app.on_message(
- filters.text
- & filters.reply
- & ~filters.bot
- & ~filters.via_bot
- & ~filters.forwarded,
- group=chatbot_group,
- )
- @capture_err
- async def chatbot_talk(_, message: Message):
- db = await check_chatbot()
- if message.chat.id not in db["bot"]:
- return
- if not message.reply_to_message:
- return
- if not message.reply_to_message.from_user:
- return
- if message.reply_to_message.from_user.id != BOT_ID:
- return
- await type_and_send(message)
- # FOR USERBOT
- @app2.on_message(
- filters.command("chatbot", prefixes=USERBOT_PREFIX)
- & SUDOERS
- & ~filters.forwarded
- & ~filters.via_bot
- )
- @capture_err
- async def chatbot_status_ubot(_, message: Message):
- if len(message.text.split()) != 2:
- return await eor(
- message,
- text=f"**用法:**\n{USERBOT_PREFIX}chatbot [开启|关闭]",
- )
- await chat_bot_toggle(message, is_userbot=True)
- @app2.on_message(
- ~filters.me & ~filters.private & filters.text,
- group=chatbot_group,
- )
- @capture_err
- async def chatbot_talk_ubot(_, message: Message):
- db = await check_chatbot()
- if message.chat.id not in db["userbot"]:
- return
- username = "@" + str(USERBOT_USERNAME)
- if message.reply_to_message:
- if not message.reply_to_message.from_user:
- return
- if (
- message.reply_to_message.from_user.id != USERBOT_ID
- and username not in message.text
- ):
- return
- else:
- if username not in message.text:
- return
- await type_and_send(message)
- @app2.on_message(
- filters.text & filters.private & ~filters.me,
- group=(chatbot_group + 1),
- )
- @capture_err
- async def chatbot_talk_ubot_pm(_, message: Message):
- db = await check_chatbot()
- if message.chat.id not in db["userbot"]:
- return
- await type_and_send(message)
|