ctx.voice_client.pause()导致bot离开语音呼叫,而不是暂停音频



我想弄清楚为什么我的机器人离开语音通道,而不是暂停当前播放的音频。以下是我的代码以及我尝试过的其他代码:

@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()

我也试过了:

@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.")

然而,这得到了与之前相同的结果。当机器人从调用断开连接时也不会抛出任何错误。如有任何帮助,不胜感激。

我发现你在代码中的错误。在play命令中,检查客户端是否正在播放,如果没有则断开连接。这是唯一的问题。要修复它,你只需要改变你的while循环。你可以复制我的代码(包括解释),或者直接编辑你的。

文档使用:

Discord.py文档代码:

@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!")

相关内容

  • 没有找到相关文章

最新更新