-2

我一直在尝试制作队列命令。这成功地将输入附加到 dict,例如 ['song1', 'song2']。但是尝试播放下一首歌曲而不等待当前歌曲播放完,导致已经播放错误。

async def queue(ctx):
 ctx.message.guild.name = []
 await ctx.message.channel.send('**-start of queue-**')
 await ctx.message.channel.send('**-type end when done-**')
 def ch(m):
  return m.author == ctx.message.author and m.channel == ctx.message.channel
 while True:
  song = await client.wait_for('message', check=ch, timeout=30)
  song_str = str(song.content)
  song_f = song_str.translate({ord(c): None for c in string.whitespace})
  if song_f == 'end':
   print(ctx.message.guild.name)
   break
  ctx.message.guild.name.append(song_str)
 vc = ctx.message.guild.voice_client
 for song in ctx.message.guild.name:
  player = await YTDLSource.from_url(song, loop=client.loop)
  print(song)
  vc.play(player)

4

1 回答 1

1

这是一种方法:
使用此代码,您的机器人将不会下载歌曲,他将能够播放来自链接和 youtube 搜索的歌曲。

import discord
from discord.ext import commands
from discord.utils import get

import youtube_dl
import requests

song_queue = []
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist':'True'}

#Search videos from key-words or links
def search(arg):
    try: requests.get("".join(arg))
    except: arg = " ".join(arg)
    else: arg = "".join(arg)
    with youtube_dl.YoutubeDL(YDL_OPTIONS ) as ydl:
        info = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
        
    return {'source': info['formats'][0]['url'], 'title': info['title']}

#Plays the next song in the queue
def play_next(ctx):
    voice = get(client.voice_clients, guild=ctx.guild)
    if len(song_queue) > 1:
        del song_queue[0]
        voice.play(discord.FFmpegPCMAudio(song_queue[0][source], **FFMPEG_OPTIONS), after=lambda e: play_next(ctx))
        voice.is_playing()

@client.command()
async def play(ctx, *arg):
    channel = ctx.message.author.voice.channel

    if channel:
        voice = get(client.voice_clients, guild=ctx.guild)
        song = search(arg)
        song_queue.append(song)

        if voice and voice.is_connected():
            await voice.move_to(channel)
        else: 
            voice = await channel.connect()

        if not voice.is_playing():
           voice.play(discord.FFmpegPCMAudio(song[0]['source'], **FFMPEG_OPTIONS), after=lambda e: play_next(ctx))
            voice.is_playing()
        else:
            await ctx.send("Added to queue")
    else:
        await ctx.send("You're not connected to any channel!")
于 2020-06-25T20:27:44.770 回答