0

我想确保当音乐改变时,我的 bote 状态也会改变,但是因为 next 是一个同步函数,所以我不能。这是我的代码

async def changemusique(ctx,musique):
    await ctx.send(f"Je lance : [ {musique} ]")
    await bot.change_presence(status =random.choice(statuses) ,activity=discord.Activity(type=discord.ActivityType.listening, name=musique)) 

def play_song(client, queue, song,ctx):
    source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(song.stream_url, before_options = "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"))

    def next(_):
        if len(queue) > 0:
            new_song = queue[0]
            del queue[0]
            play_song(client, queue, new_song,ctx)
            changeStatus.cancel()
            print(new_song.title)
            
        else:
            asyncio.run_coroutine_threadsafe(client.disconnect(), bot.loop)
            changeStatus.start()

    client.play(source,after=next)


@bot.command()
async def p(ctx, *args ):
    phrase =""
    if len(args)==0:
        ctx.send(f"{prefix}p [musique]")
    else:
        for x in args:
            print(x)
            phrase += f"{x}+"
    html = urllib.request.urlopen(f"https://www.youtube.com/results?search_query={phrase}")
    video_ids = re.findall(r"watch\?v(\S{12})",html.read().decode())
    print(f"https://www.youtube.com/watch?v{video_ids[0]}")
    await play(ctx,f"https://www.youtube.com/watch?v{video_ids[0]}")
    


async def play(ctx, url):
    print("play")
    client = ctx.guild.voice_client

    if client and client.channel:
        video = Video(url)
        musics[ctx.guild].append(video)
        await ctx.channel.purge(limit=1)
        await ctx.send(f"[ {video.title} ] ajouté à la liste")

    else:
        channel = ctx.author.voice.channel
        video = Video(url)
        musics[ctx.guild] = []
        client = await channel.connect()
        await ctx.channel.purge(limit=1)
        await ctx.send(f"Je lance : [ {video.title} ]")
        changeStatus.cancel()
        await bot.change_presence(status =random.choice(statuses) ,activity=discord.Activity(type=discord.ActivityType.listening, name=video.title))
        print(video.title)
        play_song(client, musics[ctx.guild], video,ctx)

p 允许使用来自 discord 的命令,并将启动播放功能,该功能本身将寻找 play_song 并且在这个完成的音乐中,它传递到下一个,在 print(new_song.title) 我可以看到我需要的值现在我只需要用这个值更改 change_presence 中的 name 属性。

4

0 回答 0