def player_play(vc: discord.VoiceClient, m: str): global nc global mQueue global isPlay ret = "" if m == "#play" or m == "#p": # 再開 if vc.is_paused(): vc.resume() ret = "Resuming!" # キュー追加 elif m.find("https://www.nicovideo.jp/watch/") >= 0: ret = "add `{0}`".format(mQueue.addQueue(m.split(" ")[1])) # 新規再生 if not vc.is_playing(): if mQueue.popQueue() == mQueue.notPlaying: return src = nc.getVideo(mQueue.nowPlaying["url"]) while os.path.getsize(src) < 256: pass vc.play(discord.FFmpegPCMAudio(src)) vc.source = discord.PCMVolumeTransformer(vc.source) vc.source.volume = 0.1 if not isPlay: isPlay = True t1 = threading.Thread(target=playcheck, args=(vc, 0)) t1.start() ret = "Play {0}".format(mQueue.now()) return ret
def playcheck(vc: discord.VoiceClient, dummy): global isPlay global nc global mQueue if isPlay: print("start") while isPlay: if vc.is_paused() == False: time.sleep(0.5) if vc.is_playing() == False: if mQueue.popQueue() != mQueue.notPlaying: src = nc.getVideo(mQueue.nowPlaying["url"]) while os.path.getsize(src) < 128: pass vc.play(discord.FFmpegPCMAudio(src)) vc.source = discord.PCMVolumeTransformer(vc.source) vc.source.volume = 0.1 isPlay = True
async def queue_task(self, ctx: commands.Context, voice_client: discord.VoiceClient): # 왜인지는 모르겠는데 매우 불안정하네요. while voice_client.is_connected(): if ctx.guild.id not in self.queues.keys(): if voice_client.is_playing(): voice_client.stop() break try: is_loop = bool(self.queues[ctx.guild.id]["playing"]["loop"]) is_shuffle = bool( self.queues[ctx.guild.id]["playing"]["random"]) except KeyError: is_loop = False is_shuffle = False if not voice_client.is_playing() and not voice_client.is_paused( ) and (not is_loop and len(self.queues[ctx.guild.id]) <= 1): break if voice_client.is_playing() or voice_client.is_paused(): await asyncio.sleep(1) continue print(self.queues) song_id_list = [ x for x in self.queues[ctx.guild.id].keys() if x != "playing" ] next_song_id = ( sorted(song_id_list)[0] if not is_shuffle else random.choice(song_id_list)) if not is_loop else "playing" print(next_song_id) next_song = self.queues[ctx.guild.id][next_song_id].copy() embed = discord.Embed( title="유튜브 음악 재생 - 재생 시작", description= f"업로더: [`{next_song['vid_author']}`]({next_song['vid_channel_url']})\n" f"제목: [`{next_song['vid_title']}`]({next_song['vid_url']})", color=discord.Color.red(), timestamp=datetime.datetime.now( tz=datetime.timezone(datetime.timedelta(hours=9)))) embed.set_footer(text=str(next_song['req_by']), icon_url=next_song['req_by'].avatar_url) embed.set_image(url=next_song['thumb']) voice_client.play( discord.FFmpegPCMAudio(next_song["tgt_url"], before_options=get_youtube.before_args)) voice_client.source = discord.PCMVolumeTransformer( voice_client.source) voice_client.source.volume = self.queues[ ctx.guild.id]["playing"]["vol"] await ctx.send(embed=embed) if not is_loop: for k, v in next_song.items(): self.queues[ctx.guild.id]["playing"][k] = v del self.queues[ctx.guild.id][next_song_id] await asyncio.sleep(1) if ctx.guild.id in self.queues.keys(): del self.queues[ctx.guild.id] if voice_client.is_connected(): await voice_client.disconnect() await ctx.send( f"대기열이 비어있고 모든 노래를 재생했어요. `{voice_client.channel}`에서 나갈께요.")
async def queue_task(bot: CustomClient, ctx: commands.Context, voice: discord.VoiceClient): while True: exists = os.path.isfile(f"music/{ctx.guild.id}.json") if not exists: await ctx.send(f"`{voice.channel}`에서 나갈께요.") await voice.disconnect(force=True) break queue = await get_queue(ctx.guild.id) if voice.is_playing() or voice.is_paused(): await asyncio.sleep(1) continue elif not voice.is_playing() and len( queue.keys()) == 1 and queue["playing"]["loop"] is not True: await ctx.send(f"음악이 끝났어요. `{voice.channel}`에서 나갈께요.") await voice.disconnect(force=True) os.remove(f"music/{ctx.guild.id}.json") break vol = queue["playing"]["vol"] if queue["playing"]["loop"] is True: tgt_url = queue["playing"]["tgt_url"] voice.play( discord.FFmpegPCMAudio(tgt_url, before_options=get_youtube.before_args)) voice.source = discord.PCMVolumeTransformer(voice.source) voice.source.volume = vol await asyncio.sleep(3) continue if queue["playing"]["random"]: queue_keys = [str(x) for x in queue.keys() if not x == "playing"] tgt_name = str(random.choice(queue_keys)) tgt_queue = queue[tgt_name] else: tgt_name = list(queue.keys())[1] tgt_queue = queue[tgt_name] vid_url = tgt_queue["vid_url"] vid_title = tgt_queue["vid_title"] vid_author = tgt_queue["vid_author"] vid_channel_url = tgt_queue["vid_channel_url"] tgt_url = tgt_queue["tgt_url"] thumb = tgt_queue["thumb"] req_by = bot.get_user(int(tgt_queue["req_by"])) voice.play( discord.FFmpegPCMAudio(tgt_url, before_options=get_youtube.before_args)) voice.source = discord.PCMVolumeTransformer(voice.source) voice.source.volume = vol embed = discord.Embed(title="유튜브 음악 재생", color=discord.Colour.from_rgb(255, 0, 0)) embed.add_field( name="재생 시작", value= f"업로더: [`{vid_author}`]({vid_channel_url})\n제목: [`{vid_title}`]({vid_url})", inline=False) embed.add_field(name="요청자", value=f"{req_by.mention} (`{req_by}`)", inline=False) embed.set_image(url=str(thumb)) queue["playing"]["vid_url"] = vid_url queue["playing"]["vid_title"] = vid_title queue["playing"]["vid_author"] = vid_author queue["playing"]["vid_channel_url"] = vid_channel_url queue["playing"]["thumb"] = thumb queue["playing"]["tgt_url"] = tgt_url queue["playing"]["req_by"] = tgt_queue["req_by"] await ctx.send(embed=embed) del queue[tgt_name] await update_queue(ctx.guild.id, queue) await asyncio.sleep(3)