Как сделать Discord Bot l oop audio? [discord.py] - PullRequest
0 голосов
/ 18 июня 2020

Я хочу, чтобы бот воспроизводил фрагмент звука, и, когда звук закончится, он будет воспроизводить звук.

Что у меня есть:

@client.command()
async def play(ctx):
await ctx.channel.purge(limit=1)
channel = ctx.author.voice.channel
if channel:
    print(channel.id)
    await channel.connect()
guild = ctx.guild
audio_source = discord.FFmpegPCMAudio('audio.mp3')
voice_client: discord.VoiceClient = discord.utils.get(client.voice_clients, guild=guild)
if not voice_client.is_playing():
    voice_client.play(audio_source, after=None)

1 Ответ

2 голосов
/ 19 июня 2020

Вам просто нужно создать новую функцию, которая будет воспроизводиться, когда звук закончится, например, repeat:

@client.command()
async def play(ctx):
    await ctx.channel.purge(limit=1)
    channel = ctx.author.voice.channel
    voice = get(self.bot.voice_clients, guild=ctx.guild)

    def repeat(ctx):
        voice = get(self.bot.voice_clients, guild=ctx.guild)
        voice.play(discord.FFmpegPCMAudio('audio.mp3', after=lambda e: repeat(ctx))
        voice.is_playing()

    if channel and not voice.is_playing():
        voice.play(discord.FFmpegPCMAudio('audio.mp3', after=lambda e: repeat(ctx))
        voice.is_playing()
...