async def bababooey(bot, message): author_voice_status = message.author.voice if author_voice_status is not None and author_voice_status.channel is not None: bababooey_audio_stream = FFmpegPCMAudio( 'bossbot/resources/bababooey.mp3') voice_client = await author_voice_status.channel.connect() voice_client.play(bababooey_audio_stream) while voice_client.is_playing(): sleep(1) bababooey_audio_stream.cleanup() await voice_client.disconnect() else: await message.channel.send( 'https://www.youtube.com/watch?v=U_cPir6MwLM')
class YandexAudioSource(PCMVolumeTransformer): def __init__(self, track: Track, volume=0.5): self.track = track self.volume = volume self.original = None self._is_downloaded = False def __str__(self): return self.full_title @property def full_title(self) -> str: title, artists = self.track.title, ', '.join( a.name for a in self.track.artists if a.name) return f'{title} - {artists}' @property def file_name(self) -> str: if self.track.albums: return f'{self.track.id}_{self.track.albums[0].id}.mp3' return f"{self.track.id}.mp3" def download(self): if not os.path.isfile(self.file_name): self.track.download(self.file_name, bitrate_in_kbps=192) self.original = FFmpegPCMAudio(self.file_name) self._is_downloaded = True def cleanup(self): if self._is_downloaded: self.original.cleanup() def read(self): if not self._is_downloaded: self.download() return super().read()
async def player_loop(self): """Our main player loop.""" await self.bot.wait_until_ready() while not self.bot.is_closed(): self.next.clear() # if self.queue.empty(): # generate a song to autoplay try: # Wait for the next song. If we timeout cancel the player and disconnect... async with timeout(300): # 5 minutes... video: Video = await self.queue.get() except asyncio.TimeoutError: return self.destroy(self._guild) self.current = video if video.file: player = FFmpegPCMAudio("/bot/assets/audio/" + video.filename, executable=config.FFMPEG_PATH) else: player, self.current.video_length = await YTDLSource.from_url( video.video_url, loop=self.bot.loop, stream=True) video.time_started = int(time.time()) self._guild.voice_client.play( player, after=lambda e: asyncio.run_coroutine_threadsafe( self.after_play(error=e), loop=self.bot.loop)) await self.next.wait() # Make sure the FFmpeg process is cleaned up. player.cleanup() self.current = None
async def player_loop(self): await self.bot.wait_until_ready() while not self.bot.is_closed(): self.next_song.clear() try: if len(self.pq) == 0 and self.wait is True: source = FFmpegPCMAudio('intro.webm') source.title = 'chill' elif len(self.pq) == 0: await asyncio.sleep(5) source = self.pq.pop() else: source = self.pq.pop() except KeyError: await self._channel.send( 'There is no audio on the queue, so I will disconnect from the channel. Use the command !play or !p to queue more audios', delete_after=10) return self.destroy(self._guild) if not isinstance(source, YTDLSource) and not isinstance( source, FFmpegPCMAudio): # Source was probably a stream (not downloaded) # So we should regather to prevent stream expiration try: source = await YTDLSource.regather_stream( source, loop=self.bot.loop) except Exception as e: await self._channel.send( f'There was an error processing your song.\n' f'```css\n[{e}]\n```') continue source.volume = self.volume self.current = source print(source.title, self._guild.name) opus = ctypes.util.find_library('opus') discord.opus.load_opus(opus) if not discord.opus.is_loaded(): raise RuntimeError('Opus failed to load') self._guild.voice_client.play( source, after=lambda _: self.bot.loop.call_soon_threadsafe( self.next_song.set)) if source.title == 'chill': self.np = await self._channel.send( 'Please, listen to this chill music until your audio is ready to play' ) else: self.np = await self._channel.send( 'Playing now: {a} - Requested by: <@{b}>'.format( a=source.title, b=source.requester.author.id)) await self.next_song.wait() source.cleanup() self.current = None try: # We are no longer playing this song... await self.np.delete() except discord.HTTPException: pass