【问题标题】:How to make the discord bot repeat the song playback?如何让不和谐机器人重复播放歌曲?
【发布时间】:2020-08-30 03:29:22
【问题描述】:

我想制作一个不和谐的机器人来重复播放歌曲。假设变量 count 包含我希望歌曲重复的次数,我如何让这个命令 voice.play(discord.FFmpegPCMAudio(audio)) 重复 n 次,确保每次重复仅在歌曲完全播放后发生?

【问题讨论】:

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


    【解决方案1】:

    VoiceClient.play() 有一个 after 参数,可用于再次播放音频:

    from discord.ext import commands
    from asyncio import run_coroutine_threadsafe as rct
    
    bot = commands.Bot(prefix='your_prefix')
    
    def play_next(ctx, audio, msg, n):
        if n:
            voice = get(bot.voice_clients, guild=ctx.guild)
            rct(msg.edit(content='Finished playing the song, {n} more to go.'), bot.loop)
            voice.play(FFmpegPCMAudio(audio), after=lambda e: play_next(ctx, audio, msg, n-1))
            voice.is_playing()
        else:
            rct(msg.delete())
    
    @bot.command()
    repeat(ctx, n):
        audio = 'your_audio_source'
        voice = get(bot.voice_clients, guild=ctx.guild)
    
        msg = await ctx.send(f'Started playing video {n} times')
        voice.play(FFmpegPCMAudio(audio), after=lambda e: play_next(ctx, audio, msg, n-1))
        voice.is_playing()
    
    bot.run('your_token')
    

    PS:这段代码中没有错误管理,你必须自己做。

    【讨论】:

    • 感谢您的帮助。但是,我收到此错误。 NameError: name 'self' is not defined。我该如何解决?
    • 我的错,我拿了一些我的代码^^。您只需从voice = get(self.bot.voice_clients, guild=ctx.guild)run_coroutine_threadsafe(ctx.send('Finished playing the song, {n} more to go.'), self.bot.loop) 中删除self.
    • 下一次迭代如何删除ctx.send('Finished playing the song, {n} more to go.')发送的消息?我试图保持不和谐消息的干净,只显示一条消息。
    • 我更改了代码,所以它会发送一条消息,编辑它并在完成后将其删除:)
    • 如果你告诉 voice.is_playing() 命令的作用会很棒
    猜你喜欢
    • 2021-09-06
    • 1970-01-01
    • 2021-10-20
    • 2021-12-10
    • 2021-05-10
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多