Python 断开音乐机器人与语音频道的连接

Python 断开音乐机器人与语音频道的连接,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我有这个作为我的音乐课,让我的机器人播放音乐 class Music(commands.Cog): def __init__(self, bot): self.bot = bot self.bot.music = lavalink.Client(self.bot.user.id) self.bot.music.add_node("localhost", 7000, 'server-utils', 'eu', 'music

我有这个作为我的音乐课,让我的机器人播放音乐

class Music(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.bot.music = lavalink.Client(self.bot.user.id)
        self.bot.music.add_node("localhost", 7000, 'server-utils', 'eu', 'music-node')
        self.bot.add_listener(self.bot.music.voice_update_handler, 'on_socket_response')
        self.bot.music.add_event_hook(self.track_hook)

    @commands.command(name="music")
    async def music(self, ctx, opt, *, arg=None):
        if opt == "join":
            print(f"music join command worked")
            member = discord.utils.find(lambda m: m.id == ctx.author.id, ctx.guild.members)
            if member is not None and member.voice is not None:
                vc = member.voice.channel
                player = self.bot.music.player_manager.create(ctx.guild.id, endpoint=str(ctx.guild.region))
                if not player.is_connected:
                    player.store('channel', ctx.guild.id)
                    await self.connect_to(ctx.guild.id, str(vc.id))
        if opt == "play" and arg is not None:
            try:
                player = self.bot.music.player_manager.get(ctx.guild.id)
                query = f'ytsearch:{arg}'
                results = await player.node.get_tracks(query)
                tracks = results['tracks'][0:10]
                i = 0
                query_result = ''
                for track in tracks:
                    i = i + 1
                    query_result = query_result + f'{i}) {track["info"]["title"]}\n'
                embed = discord.Embed()
                embed.description = query_result

                await ctx.channel.send(embed=embed)

                def check(m):
                    return m.author.id == ctx.author.id

                reponse = await self.bot.wait_for('message', check=check)
                track = tracks[int(reponse.content)-1]

                player.add(requester=ctx.author.id, track=track)
                if not player.is_playing:
                    await player.play()
            except Exception as error:
                print(error)

        if opt == "stop":
            try:
                player = self.bot.music.player_manager.get(ctx.guild.id)
                if player.is_playing:
                    await player.stop()
            except Exception as error:
                print(error)

        if opt == "leave":
            player = self.bot.music.player_manager.get(ctx.guild.id)
            if player.is_playing:
                await player.stop()
            await self.disconnect_from(ctx.guild.id)

    async def track_hook(self, event):
        if isinstance(event, lavalink.events.QueueEndEvent):
            guild_id = int(event.player.guild_id)
            await self.connect_to(guild_id, None)

    async def connect_to(self, guild_id: int, channel_id: int):
        ws = self.bot._connection._get_websocket(guild_id)
        await ws.voice_state(str(guild_id), channel_id)

    async def disconnect_from(self, guild_id: int):
        ws = self.bot._connection.voice_clients
        print(ws)



def setup(bot):
    bot.add_cog(Music(bot))
除了与他连接的频道断开连接的部分外,所有的链接都可以正常工作。。。我尝试了很多方法,但都不知道如何使其断开。。
Ps:这里的命令除了打印一个列表之外什么都没做,我希望那里会有机器人连接,但是那里什么都没有。我相信这是因为你传递的是公会id而不是语音频道id,例如

你在公会里;你好,世界号12345 你在协会的语音频道;Hello World语音频道,其id为6789


因此,您正在尝试使用guild.id而不是语音频道id。这有意义吗?

您不能执行
等待ctx.voice\u client.disconnect()

如果opt==“离开”:
player=self.bot.music.player\u manager.get(ctx.guild.id)
如果player.u正在播放:
等待玩家。停止()
等待ctx.voice_client.disconnect()

我使用VoiceClient.disconnect()在我的Discord bot上执行了断开连接命令。在第一种方法中,我遇到了一些问题,因为connect命令生成了VoiceClient对象,但是disconnect命令无法使用它,因为它是另一个命令上的局部变量,所以我只是将其设置为全局变量,并且可以工作

@commands.command()
async def enter(self, ctx):
    try:
        canal = ctx.author.voice.channel
        global voice_client
        voice_client = await canal.connect()

    except AttributeError:
        await ctx.send("Você precisa estar conectado a um canal de voz")

    except discord.errors.ClientException:
        await ctx.send("Eu já estou conectado ao seu canal de voz")

@commands.command()
async def leave(self, ctx):
    try:
        await voice_client.disconnect()

    except NameError:
        await ctx.send("Eu não estou conectado a nenhum canal de voz :worried:")
我编码的完整音乐齿轮:

有点。。。我想我已经试过了,但我会再试一次,只是为了确定,然后我会和你联系,告诉你结果是的,就像我想的那样。当我尝试使用它时,我得到了这个错误:这意味着bot没有连接到调用命令的服务器中的vc中。问题是他已连接。他和我在同一个风险投资公司。因为某种原因他认为他不是。。。