python-3.x - 如何让 discord bot 队列本地 mp3?

标签 python-3.x ffmpeg discord discord.py repl.it

我是 python 的新手,所以我想知道是否有办法这样做。

这是我播放 mp3 的命令:

@bot.command()
async def play_song1(ctx):
  global voice
  channel = ctx.message.author.voice.channel
  voice = get(bot.voice_clients, guild=ctx.guild)

  if voice and voice.is_connected():
    await voice.move_to(channel)
  else:
    voice = await channel.connect()
    voice.play(discord.FFmpegPCMAudio('./mp3/song1.mp3'))
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.1
    await ctx.send ('playing')
    while voice.is_playing():
      await asyncio.sleep(.1)  
    await voice.disconnect()

除了 song2song3,我又做了 2 个相同的命令,现在我想在有人调用它们时将 mp3 排队。

最佳答案

看起来好像你没有使用齿轮,你可以尝试这样的事情:

guild_queues = {}  # Multi-server support as a dict, just use a list for one server

# EDIT: Map song names in a dict
play_messages = {
    "song1": "Now playing something cool!",
    "song2": "And something even cooler just started playing!",
    "song3": "THE COOLEST!"
}


async def handle_queue(ctx, song):
    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    channel = ctx.author.voice.channel
    if voice and channel and voice.channel != channel:
        await voice.move_to(channel)
    elif not voice and channel:
        voice = await channel.connect()
    if not voice.is_playing():
        audio = discord.FFmpegPCMAudio(f"./{song}.mp3")
        source = discord.PCMVolumeTransformer(audio)
        source.volume = 0.1
        voice.play(audio)
        await ctx.send(play_messages[song])
        while voice.is_playing():
            await asyncio.sleep(.1)
        if len(guild_queues[ctx.guild.id]) > 0:
            next_song = guild_queues[ctx.guild.id].pop(0)
            await handle_queue(ctx, next_song)
        else:
            await voice.disconnect()


@bot.command()
async def play(ctx, *, song):  # I'd recommend adding the filenames as an arg, but it's up to you
    # Feel free to add a check if the filename exists
    try:
        # Get the current guild's queue
        queue = guild_queues[ctx.guild.id]
    except KeyError:
        # Create a queue if it doesn't already exist
        guild_queues[ctx.guild.id] = []
        queue = guild_queues[ctx.guild.id]

    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    queue.append(song)
    # The one song would be the one currently playing
    if voice and len(queue) > 0:
        await ctx.send("Added to queue!")
    else:
        current_song = queue.pop(0)
        await handle_queue(ctx, current_song)

引用资料:

关于python-3.x - 如何让 discord bot 队列本地 mp3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65538940/

相关文章:

python-3.x - Python-pydub : bad sample width error when exporting AudioSegment created from numpy array

python - 如何根据列中的值合并列中的值

video - 如何在使用 FFMPEG 合并音频和视频时减小结果视频的大小?

python ffmpeg : overlaying videos is dropping all audio

javascript - 获取超过 100 条消息

python - 从txt文件中读取和写入数字

python - 搜索最不像一组位串的位串

ffmpeg - 合并两个命令

javascript - 找不到模块discord.js-commando

python-3.x - 如何从不和谐的网络 block 嵌入中获取文本