async def create_source(cls, member: nextcord.Member, search: str, *, bot: commands.Bot, download=False): loop = bot.loop or asyncio.get_event_loop() to_run = partial(ytdl.extract_info, url=search, download=download) data = await loop.run_in_executor(None, to_run) if "entries" in data: data = data["entries"][0] if download: source = ytdl.prepare_filename(data) else: return { "webpage_url": data["webpage_url"], "requester": member, "title": data["title"], } logger.info(f"create_source() : Created Source {source}") return cls(nextcord.FFmpegPCMAudio(source), data=data, requester=member)
async def read_message(self, interaction: Interaction, voice: str, message: Message): source = tts(message.content, voice) if source is None: return await interaction.send("오류가 발생했습니다.", ephemeral=True) logger.info( f"[TTS] author : {interaction.user}, voice : {voice}, text : {message.content}" ) voice_client = interaction.guild.voice_client if voice_client is None: if interaction.user.voice: voice_client = await interaction.user.voice.channel.connect( reconnect=True) else: if interaction.user.voice: await voice_client.move_to(interaction.user.voice.channel) if source is not None: while voice_client.is_playing(): await asyncio.sleep(0.1) voice_client.play( nextcord.PCMVolumeTransformer( original=nextcord.FFmpegPCMAudio(source), volume=Config.volume_tts)) await interaction.send(content="**Read**", ephemeral=True)
async def from_url(cls, url, *, loop=None): loop = loop or asyncio.get_event_loop() data = await loop.run_in_executor( None, lambda: ytdl.extract_info(url, download=False)) print(data) if 'entries' in data: data = data['entries'][0] return cls(nextcord.FFmpegPCMAudio(data['url'], **{'options': '-vn'}), data=data)
async def stream(cls, data, *, loop): """動画ストリーム用データ取得""" loop = loop or asyncio.get_event_loop() data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url=data["webpage_url"], download=False)) return cls( discord.FFmpegPCMAudio( data['url'], **ffmpeg_options ), data=data )
async def from_url(cls, url, *, loop=None, stream=False, option=None): loop = loop or asyncio.get_event_loop() data = await loop.run_in_executor( None, lambda: ytdl.extract_info(url, download=not stream)) if 'entries' in data: # take first item from a playlist data = data['entries'][0] filename = data['url'] if stream else ytdl.prepare_filename(data) return cls(nextcord.FFmpegPCMAudio(filename, **option), data=data)
async def regather_stream(cls, data, *, loop): loop = loop or asyncio.get_event_loop() requester = data["requester"] to_run = partial(ytdl.extract_info, url=data["webpage_url"], download=False) data = await loop.run_in_executor(None, to_run) return cls(nextcord.FFmpegPCMAudio(data["url"]), data=data, requester=requester)
async def from_url(cls, url, *, loop=None, stream=False): """Downloads audio from Youtube and returns player""" loop = loop or asyncio.get_event_loop() data = await loop.run_in_executor( None, lambda: ytdl.extract_info(url, download=not stream)) if "entries" in data: # Take first item from a playlist data = data["entries"][0] filename = data["url"] if stream else ytdl.prepare_filename(data) return cls(nextcord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
async def play(self) -> None: if not self.connection or not self.connection.is_connected( ) or self.connection.is_playing() or not self.now_playing: return if "removed" in self.now_playing: await self.next(message=False) return try: if not self.now_playing.get('stream'): info = await self.ytdl.process_entry(self.now_playing) info = self.ytdl.parse_info(info) self.now_playing = {**self.now_playing, **info} song = nextcord.FFmpegPCMAudio( self.now_playing['stream'], before_options=None if not self.now_playing['is_live'] else FFMPEG_OPTIONS, ) source = nextcord.PCMVolumeTransformer( song, volume=self.get_config("volume") / 100) def after(error: Exception) -> None: if error: log.warning(f"After play error: {error}") if self.after: self.bot.loop.create_task(self.after()) self.after = self.next self.connection.play(source, after=after) except Exception as e: msg = str(e) if isinstance( e, nextcord.ClientException) else "Error while playing the song." log.exception(msg, e) await self.ctx.send(embed=Embed(msg)) else: await self.playing_message()
async def check_tts_message(message): if message.content.startswith("dc"): voice_client = message.guild.voice_client if voice_client is not None: await voice_client.disconnect() keyword = get_keyword_info(message.content) if keyword or message.content.startswith(Config.prefix): voice_client = message.guild.voice_client source = None if keyword: source = Config.SERVER_URL + Config.ENDPOINTS["FILES"] + keyword logger.info( f"author : {message.author}, voice : {keyword}, text : {message.content}" ) else: voice = get_voice(message.content[1:2]) if voice: source = tts(message.content[2:], voice) logger.info( f"[TTS] author : {message.author}, voice : {voice}, text : {message.content[2:]}" ) if voice_client is None: if message.author.voice: voice_client = await message.author.voice.channel.connect( reconnect=True) else: if message.author.voice: await voice_client.move_to(message.author.voice.channel) if source is not None: while voice_client.is_playing(): await asyncio.sleep(0.1) voice_client.play( nextcord.PCMVolumeTransformer( original=nextcord.FFmpegPCMAudio(source), volume=Config.volume_tts))
def _create_audio_source(track, volume): audio_source = nextcord.PCMVolumeTransformer( nextcord.FFmpegPCMAudio(track.src, options='-loglevel quiet')) audio_source.volume = volume return audio_source
def revive(cls, player): """Revives player (essentially rewinds audio file to beginning)""" filename = player.ytdlData["url"] return cls(nextcord.FFmpegPCMAudio(filename, **ffmpeg_options), data=player.ytdlData)