【问题标题】:ctx.voice_client.pause() causes bot to leave the voice call rather than to pause the audioctx.voice_client.pause() 导致机器人离开语音通话而不是暂停音频
【发布时间】:2021-03-07 15:55:29
【问题描述】:

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

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

但是,这实现了与以前相同的结果。该机器人在断开呼叫时也不会引发任何错误。任何帮助将不胜感激。

【问题讨论】:

    标签: python-3.x discord discord.py


    【解决方案1】:

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

    使用的文档:

    Discord.py Documentation

    代码:

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

    【讨论】:

    • 非常感谢它完美运行!你不知道你帮了我多少!非常感谢!
    猜你喜欢
    • 2021-04-24
    • 2019-04-08
    • 2018-06-16
    • 2020-10-24
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    相关资源
    最近更新 更多