pipes.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """
  2. MIT License
  3. Copyright (c) 2021 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. import asyncio
  21. from pyrogram import filters
  22. from pyrogram.types import Message
  23. from wbb import BOT_ID, SUDOERS, USERBOT_ID, app, app2
  24. from wbb.core.decorators.errors import capture_err
  25. from wbb.utils.dbfunctions import (activate_pipe, deactivate_pipe,
  26. is_pipe_active, show_pipes)
  27. from wbb.utils.filter_groups import pipes_group
  28. __MODULE__ = "Pipes"
  29. __HELP__ = """
  30. **THIS MODULE IS ONLY FOR DEVS**
  31. Use this module to create a pipe that will forward messages of one chat/channel to another.
  32. /activate_pipe [FROM_CHAT_ID] [TO_CHAT_ID] [BOT|USERBOT]
  33. Active a pipe.
  34. choose 'BOT' or 'USERBOT' according to your needs, this will decide
  35. which client will fetch the message from 'FROM_CHAT'.
  36. /deactivate_pipe [FROM_CHAT_ID] [TO_CHAT_ID]
  37. Deactivete a pipe.
  38. /show_pipes
  39. Show all the active pipes.
  40. """
  41. pipes_list_bot = []
  42. pipes_list_userbot = []
  43. async def load_pipes():
  44. print("[INFO]: LOADING PIPES")
  45. global pipes_list_bot, pipes_list_userbot
  46. pipes_list_bot = []
  47. pipes_list_userbot = []
  48. pipes = await show_pipes()
  49. for pipe in pipes:
  50. if pipe["fetcher"] == "bot":
  51. pipes_list_bot.append(pipe)
  52. continue
  53. pipes_list_userbot.append(pipe)
  54. loop = asyncio.get_running_loop()
  55. loop.create_task(load_pipes())
  56. @app.on_message(~filters.me, group=pipes_group)
  57. @capture_err
  58. async def pipes_worker_bot(_, message: Message):
  59. for pipe in pipes_list_bot:
  60. if pipe["from_chat_id"] == message.chat.id:
  61. await message.forward(pipe["to_chat_id"])
  62. @app2.on_message(~filters.me, group=pipes_group)
  63. @capture_err
  64. async def pipes_worker_userbot(_, message: Message):
  65. for pipe in pipes_list_userbot:
  66. if pipe["from_chat_id"] == message.chat.id:
  67. await message.forward(pipe["to_chat_id"])
  68. @app2.on_message(~filters.me, group=pipes_group)
  69. @capture_err
  70. async def pipes_worker_userbot(_, message: Message):
  71. for pipe in pipes_list_userbot:
  72. if pipe["from_chat_id"] == message.chat.id:
  73. if not message.text:
  74. m, temp = await asyncio.gather(
  75. app.listen(USERBOT_ID), message.copy(BOT_ID)
  76. )
  77. caption = f"Forwarded from `{pipe['from_chat_id']}`"
  78. caption = (
  79. f"{temp.caption}\n\n{caption}" if temp.caption else caption
  80. )
  81. await app.copy_message(
  82. pipe["to_chat_id"],
  83. USERBOT_ID,
  84. m.message_id,
  85. caption=caption,
  86. )
  87. await asyncio.sleep(10)
  88. await temp.delete()
  89. return
  90. caption = f"Forwarded from `{pipe['from_chat_id']}`"
  91. await app.send_message(
  92. pipe["to_chat_id"], text=message.text + "\n\n" + caption
  93. )
  94. @app.on_message(filters.command("activate_pipe") & filters.user(SUDOERS))
  95. @capture_err
  96. async def activate_pipe_func(_, message: Message):
  97. if len(message.command) != 4:
  98. await message.reply_text(
  99. "**Usage:**\n/activate_pipe [FROM_CHAT_ID] [TO_CHAT_ID] [BOT|USERBOT]"
  100. )
  101. return
  102. text = message.text.strip().split()
  103. from_chat = int(text[1])
  104. to_chat = int(text[2])
  105. fetcher = text[3].lower()
  106. if fetcher != "bot" and fetcher != "userbot":
  107. await message.reply_text("Wrong fetcher, see help menu.")
  108. return
  109. if await is_pipe_active(from_chat, to_chat):
  110. await message.reply_text("This pipe is already active.")
  111. return
  112. await activate_pipe(from_chat, to_chat, fetcher)
  113. await load_pipes()
  114. await message.reply_text("Activated pipe.")
  115. @app.on_message(filters.command("deactivate_pipe") & filters.user(SUDOERS))
  116. @capture_err
  117. async def deactivate_pipe_func(_, message: Message):
  118. if len(message.command) != 3:
  119. await message.reply_text(
  120. "**Usage:**\n/deactivate_pipe [FROM_CHAT_ID] [TO_CHAT_ID]"
  121. )
  122. return
  123. text = message.text.strip().split()
  124. from_chat = int(text[1])
  125. to_chat = int(text[2])
  126. if not await is_pipe_active(from_chat, to_chat):
  127. await message.reply_text("This pipe is already inactive.")
  128. return
  129. await deactivate_pipe(from_chat, to_chat)
  130. await load_pipes()
  131. await message.reply_text("Deactivated pipe.")
  132. @app.on_message(filters.command("pipes") & filters.user(SUDOERS))
  133. @capture_err
  134. async def show_pipes_func(_, message: Message):
  135. pipes = pipes_list_bot + pipes_list_userbot
  136. if not pipes:
  137. await message.reply_text("No pipe is active.")
  138. return
  139. text = ""
  140. for count, pipe in enumerate(pipes, 1):
  141. text += (
  142. f"**Pipe:** `{count}`\n**From:** `{pipe['from_chat_id']}`\n"
  143. + f"**To:** `{pipe['to_chat_id']}`\n**Fetcher:** `{pipe['fetcher']}`\n\n"
  144. )
  145. await message.reply_text(text)