如何修复我的Discord music机器人的命令错误?

2024-05-20 01:06:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为discord编程一个音乐播放器机器人,但它不工作。我希望机器人加入一个语音频道,瞬间播放一首歌,然后在一个歌曲播放列表中反复播放(开始时宣布每首歌的名称)

下面是我的代码:

import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
import time

#playlist of songs containing .wav files, artist names and length of song
playlist = {'Foriegn_Accent.wav':['Plague of Grackles (aka @borlip)', 128],'Go_Harder.wav':['ac', 234]}

#sets prefix for every discord command
client = commands.Bot(command_prefix = '/')

#logs bot into discord
@client.event
async def on_ready():
  print('The bot is now ready')
  print('--------------------')

@client.command(pass_context = True)
async def join(ctx): #user says /join and bot joins voice channel and plays music
    channel = ctx.message.author.voice.channel
    voice = await channel.connect() #joins voice channel
    for song in playlist: #loop through songs in playlist
        source = FFmpegPCMAudio(song) #reads wav file
        player = voice.play(source) #plays audio in voice channel
        song = song.replace('_'," ")[:3] #song name
        artist = playlist[song][0] #artist name
        length = playlist[song][1] #length of song in seconds
        await ctx.send(f'Playing {song} by {artist}') #sends message in text channel
        time.sleep(length) #waits for song to finish before next iteration

当我运行代码时,bot加入频道,播放播放列表中的第一首歌曲,并且不执行任何进一步的任务。它向我提供了以下错误消息:

上述异常是以下异常的直接原因:

Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/anaconda3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/anaconda3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'For'
Guessed Channel Layout for Input Stream #0.0 : stereo

一段时间后,最后一行更改为此消息:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TimeoutError:

这个错误消息意味着什么,我如何让我的机器人完成它的其余任务


Tags: ofinimportsongartistbotchannelplaylist