I'm making a discord music bot that play audio from youtube without downloading, the play function from file music_cog looks like this:
async def play_next(self, ctx, path):
voice = discord.utils.get(self.client.voice_clients, guild=ctx.guild)
lst = open(path + "/list.txt", "r")
urls = lst.readlines()
lst.close()
if len(urls) > 1:
del urls[0]
lst2 = open(path + "/list.txt", "w+")
for url in urls:
lst2.write(url)
lst2.close()
url = urls[0]
player = await YTDLSource.from_url(url, loop=self.client.loop, stream = True)
if not voice.is_playing():
ctx.voice_client.play(player, after=lambda e: self.play_next(ctx, path))
asyncio.run_coroutine_threadsafe(ctx.send("No more songs in queue."))
@commands.command(hidden=True, pass_context=True)
async def yt(self, ctx):
voiceChannel = discord.utils.get(ctx.guild.voice_channels, name=ctx.message.author.voice.channel.name)
bot_channel = ctx.guild.voice_client
if bot_channel == None:
await voiceChannel.connect()
voice = discord.utils.get(self.client.voice_clients, guild=ctx.guild)
path = "C:/Users/Laptop88_LTV/Documents/GitHub/Python/Bot/music_bot/Nymphosic/music_zone/voice_channels/" + str(voiceChannel.id)
if not os.path.exists(path):
os.makedirs(path)
lst = open(path + "/list.txt", "r")
url = lst.readline()
lst.close()
# url = 'https://www.youtube.com/watch?v=D4Fi1YBbzDY'
voice = discord.utils.get(self.client.voice_clients, guild=ctx.guild)
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.client.loop, stream = True)
if not voice.is_playing():
ctx.voice_client.play(player, after=lambda e: self.play_next(ctx, path))
await ctx.send('Now playing: {}'.format(player.title))
else:
await ctx.send("``` Already playing. ```")
However, it stops after playing the first song, the error said: C:\Users\Laptop88_LTV\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\player.py:616: RuntimeWarning: coroutine 'music_cog.play_next' was never awaited
self.after(error)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
What should I do to solve it?