首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使不和谐的bot队列本地mp3?

如何使不和谐的bot队列本地mp3?
EN

Stack Overflow用户
提问于 2021-01-02 12:10:07
回答 1查看 661关注 0票数 3

我对python并不熟悉,所以我想知道是否有办法做到这一点。

下面是我的play mp3命令:

代码语言:javascript
复制
@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进行排队。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-02 14:08:27

如果你不使用齿轮,你可以尝试这样的方法:

代码语言:javascript
复制
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)

参考文献:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65538940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档