Reputation: 27
I am trying to figure out why my bot is leaving the voice channel rather than pausing the currently playing audio. Here is my code along with other code I have tried:
@commands.command(brief = "Plays a locally hosted song.")
async def play(self, ctx, *, arg):
await ctx.send(f"Joining voice channel: **{ctx.author.voice.channel}**")
destination = ctx.author.voice.channel
await destination.connect()
await ctx.send(f"Joined voice channel: **{ctx.author.voice.channel}**")
voice = discord.utils.get(self.client.voice_clients, guild=ctx.guild)
voice.play(discord.FFmpegPCMAudio(source=f"./audio/{arg}.flac"))
while voice.is_playing():
await asyncio.sleep(.1)
await voice.disconnect()
@commands.command(brief = "Pauses the currently playing song.")
async def pause(self, ctx):
ctx.voice_client.pause()
I have also tried:
@commands.command(brief = "Pauses the currently playing song.")
async def pause(self, ctx):
voice = discord.utils.get(self.client.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.pause()
else:
await ctx.send("There is currently no audio playing.")
However, this achieves the same result as before. The bot is also not throwing any errors when it disconnects from the call. Any help would be appreciated.
Upvotes: 0
Views: 1724
Reputation: 26
I found your mistake in the code. In the play command you check if the client is playing and if not it disconnects. This was the only problem. To fix it you just need to change your while loop. You can copy my code(includes explanations) or just edit your one.
Documentation Used:
Code:
@commands.command(brief="Plays a locally hosted song.")
async def play(self, ctx, *, arg):
await ctx.send(f"Joining voice channel: **{ctx.author.voice.channel}**")
# Replaced Destination with ctx.voice_client, Added part where it checks if its already connected and needs to move to another channel
if ctx.voice_client is None:
await ctx.author.voice.channel.connect()
else:
await ctx.voice_client.move(ctx.author.voice.channel)
await ctx.send(f"Joined voice channel: **{ctx.author.voice.channel}**")
# Replaced voice with ctx.voice_client
ctx.voice_client.play(discord.FFmpegPCMAudio(source=f"./audio/{arg}.flac"))
# Edited Loop because it caused the Bot to disconnect
# Loop now runs if Bot is connected to channel
while ctx.voice_client.is_connected():
# checks if the bot is the only one in the channel
if len(ctx.voice_client.channel.members) == 1:
# disconnects
await ctx.voice_client.disconnect()
break
# checks if client is pause
elif ctx.voice_client.is_paused():
await asyncio.sleep(1)
# Checks if client is playing
elif ctx.voice_client.is_playing():
await asyncio.sleep(1)
# if nothing of the above is fulfilled
else:
# disconnect
await ctx.voice_client.disconnect()
break
@commands.command(brief="Pauses the currently playing song.")
async def pause(self, ctx):
# Checks if music is playing and pauses it, otherwise sends the player a message that nothing is playing
try:
ctx.voice_client.pause()
except:
await ctx.send(f"{ctx.author.mention} i'm not playing music at the moment!")
@commands.command(brief="Resumes the paused song.")
async def resume(self, ctx):
# Checks if music is paused and resumes it, otherwise sends the player a message that nothing is playing
try:
ctx.voice_client.resume()
except:
await ctx.send(f"{ctx.author.mention} i'm not playing music at the moment!")
Upvotes: 1