提问者:小点点

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


我把这个作为我的音乐课让我的机器人演奏音乐

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:这里的命令除了打印一个我希望有bot连接的列表之外什么都不做,但那里什么都没有


共3个答案

匿名用户

例如,我相信这是因为您传递的是公会ID而不是语音频道ID。

你在公会;你好世界,ID为12345。你在的公会语音频道;你好世界语音频道,id为6789。

所以你试图使用guild.id而不是语音频道ID。这有意义吗?

匿名用户

你不能做等待ctx。voice_client. disconnect()

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

匿名用户

我使用VoiceClient. disconnect()在我的Disord机器人上执行了一个断开连接命令。在我的第一种方法中,我遇到了一些麻烦,因为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:")

我编码的完整音乐齿轮:https://github.com/Voz-bonita/Discord-Bot/blob/master/Musicextension.py