chatbot.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """
  2. MIT License
  3. Copyright (c) present 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 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.filter_groups import chatbot_group
  36. from wbb.utils.dbfunctions import check_chatbot, add_chatbot, rm_chatbot
  37. __MODULE__ = "ChatBot"
  38. __HELP__ = """
  39. /chatbot [ENABLE|DISABLE] To Enable Or Disable ChatBot In Your Chat.
  40. There's one module of this available for userbot also
  41. check userbot module help."""
  42. async def chat_bot_toggle(message: Message, is_userbot: bool):
  43. status = message.text.split(None, 1)[1].lower()
  44. chat_id = message.chat.id
  45. db = await check_chatbot()
  46. db = db["userbot"] if is_userbot else db["bot"]
  47. if status == "enable":
  48. if chat_id not in db:
  49. await add_chatbot(chat_id, is_userbot=is_userbot)
  50. text = "Chatbot Enabled!"
  51. return await eor(message, text=text)
  52. await eor(message, text="ChatBot Is Already Enabled.")
  53. elif status == "disable":
  54. if chat_id in db:
  55. await rm_chatbot(chat_id, is_userbot=is_userbot)
  56. return await eor(message, text="Chatbot Disabled!")
  57. await eor(message, text="ChatBot Is Already Disabled.")
  58. else:
  59. await eor(message, text="**Usage:**\n/chatbot [ENABLE|DISABLE]")
  60. # Enabled | Disable Chatbot
  61. @app.on_message(
  62. filters.command("chatbot")
  63. )
  64. @capture_err
  65. async def chatbot_status(_, message: Message):
  66. if len(message.command) != 2:
  67. return await eor(message, text="**Usage:**\n/chatbot [ENABLE|DISABLE]")
  68. await chat_bot_toggle(message, is_userbot=False)
  69. async def lunaQuery(query: str, user_id: int):
  70. luna = await arq.luna(query, user_id)
  71. return luna.result
  72. async def type_and_send(message: Message):
  73. chat_id = message.chat.id
  74. user_id = message.from_user.id if message.from_user else 0
  75. query = message.text.strip()
  76. await message._client.send_chat_action(chat_id, "typing")
  77. response, _ = await gather(lunaQuery(query, user_id), sleep(3))
  78. await message.reply_text(response)
  79. await message._client.send_chat_action(chat_id, "cancel")
  80. @app.on_message(
  81. filters.text
  82. & filters.reply
  83. & ~filters.bot
  84. & ~filters.via_bot
  85. & ~filters.forwarded,
  86. group=chatbot_group,
  87. )
  88. @capture_err
  89. async def chatbot_talk(_, message: Message):
  90. db = await check_chatbot()
  91. if message.chat.id not in db["bot"]:
  92. return
  93. if not message.reply_to_message:
  94. return
  95. if not message.reply_to_message.from_user:
  96. return
  97. if message.reply_to_message.from_user.id != BOT_ID:
  98. return
  99. await type_and_send(message)
  100. # FOR USERBOT
  101. @app2.on_message(
  102. filters.command("chatbot", prefixes=USERBOT_PREFIX)
  103. & SUDOERS
  104. )
  105. @capture_err
  106. async def chatbot_status_ubot(_, message: Message):
  107. if len(message.text.split()) != 2:
  108. return await eor(message, text="**Usage:**\n.chatbot [ENABLE|DISABLE]")
  109. await chat_bot_toggle(message, is_userbot=True)
  110. @app2.on_message(
  111. filters.me
  112. & ~filters.private
  113. & ~filters.text,
  114. group=chatbot_group,
  115. )
  116. @capture_err
  117. async def chatbot_talk_ubot(_, message: Message):
  118. db = await check_chatbot()
  119. if message.chat.id not in db["userbot"]:
  120. return
  121. username = "@" + str(USERBOT_USERNAME)
  122. if message.reply_to_message:
  123. if not message.reply_to_message.from_user:
  124. return
  125. if (
  126. message.reply_to_message.from_user.id != USERBOT_ID
  127. and username not in message.text
  128. ):
  129. return
  130. else:
  131. if username not in message.text:
  132. return
  133. await type_and_send(message)
  134. @app2.on_message(
  135. filters.text
  136. & ~filters.private
  137. & ~filters.me,
  138. group=(chatbot_group + 1),
  139. )
  140. @capture_err
  141. async def chatbot_talk_ubot_pm(_, message: Message):
  142. db = await check_chatbot()
  143. if message.chat.id not in db["userbot"]:
  144. return
  145. await type_and_send(message)