【问题标题】:Discord.py song queuing systemDiscord.py歌曲排队系统
【发布时间】:2020-08-17 02:43:39
【问题描述】:

我正在尝试编写一个不和谐的音乐机器人,它一直在工作,直到我将排队系统从 URL 列表转换为带有歌曲名称的字典,以便我可以显示接下来播放的内容。

当第二首歌曲即将播放时,我收到以下错误消息:

C:\Users\sammy\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\player.py:611: RuntimeWarning: coroutine 'Command.__call__' was never awaited
  self.after(error)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

这里是代码,我稍微修剪了一下,让它不会太长:

global ydl_opts
ydl_opts = {
            'format': 'bestaudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }],
        }


from discord.utils import get
global voice
global queue_list
queue_list = {}

def queue():
    global queue_list
    print("hi")
    print(len(queue_list))
    if len(queue_list) != 0:
        keys = list(queue_list.keys())
        values = list(queue_list.values())
        print("AAAAAAAAAAAAA: " + str(values[0]))
        play_url(str(values[0]))
        print("AAAAAAAAAAAAA2: " + str(queue_list[keys[0]]))

        del queue_list[keys[0]]

        print(str(queue_list))

def play_url(url):
    try:
        os.remove("song.mp3")
    except:pass

    url = str(url)
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, "song.mp3")
    voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: queue())
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.07

@client.command()
async def play(ctx, curl):
    #global voice
    try:
        global voice
        voice = await ctx.message.author.voice.channel.connect()
    except:pass
    if voice.is_playing() == False:
        print("first")
        song_there = os.path.isfile("song.mp3")
        try:
            if song_there:
                os.remove("song.mp3")
        except PermissionError:
            return

        #voice = get(client.voice_clients, guild=ctx.guild)
        play_url(curl)
    elif voice and voice.is_playing():
        print("next")
        info_dict = YoutubeDL(ydl_opts).extract_info(curl, download=False)#youtube dl attribute settings are needed
        #print(info_dict['title'])
        if info_dict.get('title', None) in queue_list:
            queue_list[str(info_dict['title']) + str(random.randint())] = curl
        else:
            queue_list[str(info_dict['title'])] = curl
        #print(str(queue_list))
        pass

@client.command(pass_context=True)
async def pause(ctx):
    #voice = get(ctx.client.voice_clients, guild=ctx.guild)
    #voice = await ctx.message.author.voice.channel.connect()
    if voice and voice.is_playing():
        voice.pause()

@client.command(pass_context=True)
async def resume(ctx):
    if voice and voice.is_paused():
        voice.resume()

@client.command(pass_context=True)
async def stop(ctx):
    global queue_list
    queue_list = []
    if voice and voice.is_playing():
        voice.stop()

@client.command(pass_context=True)
async def skip(ctx):
    voice.stop()
    try:
        os.remove("song.mp3")
    except:pass
    play_url(queue_list[0])

@client.command(pass_context=True)
async def queue(ctx):
    values = list(queue_list.values())
    keys = list(queue_list.keys())
    await ctx.send("*Up Next:*")
    for i in range(len(keys)):
        info_dict = YoutubeDL(ydl_opts).extract_info(values[i], download=False)

        message = "**" + str(i + 1) + ".** " + str(keys[i]) + " (" + str(info_dict['duration']) + ")"
        await ctx.send(message)


client.run(TOKEN)


【问题讨论】:

    标签: python-3.x discord.py-rewrite youtube-dl


    【解决方案1】:

    (通常)意味着您忘记在某处的协程(以async 开头的函数)之前使用await,这是强制性的。

    【讨论】:

      【解决方案2】:

      C:\Users\sammy\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\player.py:611: RuntimeWarning: coroutine 'Command.call' 是从未期待 self.after(错误) RuntimeWarning: 启用 tracemalloc 以获取对象分配回溯

      正如您的错误代码所述,您忘记在第 611 行的开头添加“等待”。

      C:\Users\sammy\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\player.py 是脚本的文件路径

      :611: 是上述文件中的行

      RuntimeWarning: coroutine 'Command.call' was never waiting self.after(error) 是文件中的问题。

      我希望这个细分对您有所帮助并帮助您的事业。

      【讨论】:

      • 你好@St0rmWalker,不确定这是否有帮助。 player.py 是 Discord python 包 [1] 中的一个文件。您可以通过在问题中突出显示触发错误的行来改进您的答案。很可能是after参数传入的回调:voice.play(..., after=lambda e: queue())[1]github.com/Rapptz/discord.py/blob/…
      猜你喜欢
      • 2021-03-21
      • 2011-05-20
      • 2021-10-07
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多