如何使不和谐的机器人排队本地 mp3?

Posted

技术标签:

【中文标题】如何使不和谐的机器人排队本地 mp3?【英文标题】:How to make the discord bot queue local 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()

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

【问题讨论】:

@Lemon.py from discord.utils import get 【参考方案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)

参考资料:

utils.get() Client.voice_clients Context.guild Member.voice VoiceState.channel VoiceClient.move_to() VoiceClient.is_playing() VoiceClient.play() discord.FFmpegPCMAudio() discord.PCMVolumeTransformer() Context.send()

【讨论】:

@jaos1 抱歉,我才看到您的编辑!我刚刚编辑了我的答案 - 您可以将一些值映射到字典中的歌曲名称 现在它确实将歌曲添加到队列中,但它一直在循环播放第一首歌曲,为什么? @jaos1 很抱歉,我的答案更正了! @jaos1 断开线缩进不足,现在再试一次

以上是关于如何使不和谐的机器人排队本地 mp3?的主要内容,如果未能解决你的问题,请参考以下文章

如何使不和谐机器人自动发送消息?

如何使不和谐机器人将消息发送到不和谐 Node js 的特定频道机器人

如何使不和谐机器人随机跳过响应

如何使不和谐的 js 机器人在某个时间在不和谐的确切文本通道中发送随机消息(我制作一个列表并发送它)

使不和谐机器人发送消息特定通道

我需要帮助使不和谐卷曲