【问题标题】:How to make the discord bot queue local mp3?如何使不和谐的机器人排队本地 mp3?
【发布时间】:2021-04-08 20:16:42
【问题描述】:

我是 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()

除了song2 和song3 之外,我又做了两个相同的命令,现在我想在有人调用它们时将 mp3 排队。

【问题讨论】:

  • @Lemon.py from discord.utils import get

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


【解决方案1】:

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

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)

参考资料:

【讨论】:

  • @jaos1 抱歉,我才看到您的编辑!我刚刚编辑了我的答案 - 您可以将一些值映射到字典中的歌曲名称
  • 现在它确实将歌曲添加到队列中,但它一直在循环播放第一首歌曲,为什么?
  • @jaos1 很抱歉,我的答案更正了!
  • @jaos1 断开线缩进不足,现在再试一次
猜你喜欢
  • 1970-01-01
  • 2021-11-14
  • 2021-05-08
  • 2020-08-25
  • 2020-09-23
  • 2018-03-20
  • 2020-01-16
  • 1970-01-01
  • 2020-07-03
相关资源
最近更新 更多