voice_chat.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from pyrogram import filters
  2. from wbb import vc, arq, app
  3. from wbb.utils.errors import capture_err
  4. from wbb.utils.botinfo import USERBOT_ID
  5. import ffmpeg
  6. import aiohttp
  7. import aiofiles
  8. __MODULE__ = "Voice Chat"
  9. __HELP__ = """/joinvc - To Join A Voice Chat
  10. /music [SONG_NAME] - To Play Music In VC"""
  11. @app.on_message(filters.command("joinvc") & filters.user(USERBOT_ID))
  12. @capture_err
  13. async def joinvc(_, message):
  14. if vc.is_connected:
  15. await message.reply_text("__**Bot Is Already In Voice Chat.**__")
  16. return
  17. await vc.start(message.chat.id)
  18. await message.reply_text("__**Joined The Voice Chat.**__")
  19. @app.on_message(filters.command("music") & filters.user(USERBOT_ID))
  20. @capture_err
  21. async def play_music(_, message):
  22. if len(message.command) < 2:
  23. await message.reply_text("**Usage:**\n/music [SONG_NAME]")
  24. return
  25. m = await message.reply_text("__**Searching**__")
  26. query = message.text.split(None, 1)[1]
  27. song = (await arq.saavn(query))[0]
  28. await m.edit("__**Downloading**__")
  29. await download_and_transcode_song(song.media_url)
  30. await m.edit(f"__**Playing {song.song}\n**__")
  31. async def download_and_transcode_song(url):
  32. async with aiohttp.ClientSession() as session:
  33. async with session.get(url) as resp:
  34. if resp.status == 200:
  35. f = await aiofiles.open('song.mp3', mode='wb')
  36. await f.write(await resp.read())
  37. await f.close()
  38. transcode("song.mp3")
  39. def transcode(filename):
  40. ffmpeg.input(filename).output("input.raw", format='s16le',
  41. acodec='pcm_s16le', ac=2, ar='48k').overwrite_output().run()
  42. os.remove(filename)