我最近开始通过制作不和谐机器人来学习python。现在我正在制作一个音乐机器人,但我收到一个错误,找不到错误在哪里。你们能帮忙吗?它加入频道,但在聊天中给出错误3,如果我写!再次播放(URL),它在聊天中给出错误4。
这是代码:
import discord
client = discord.Client ()
@client.event
async def on_ready():
print(client.user.name)
print("-------------------")
@client.event
async def on_message(message):
if message.content.startswith("!stop"):
try:
voice_client = client_voice_in(message.server)
await voice_client.disconnect()
except AttributeError:
await client.send_message(message.channel, "I'm not connected.")
except Exception as Hugo:
await client.send_message(message.channel, "Error1_______|type|_______".format(type=Hugo))
if message.content.startswith("!play"):
try:
yt_url = message.content[6:]
if client.is_voice_connected(message.server):
try:
voice = client.voice_client_in(message.server)
players[message.server.id].stop()
player = await voice.create_ytdl_player(yt_url, before_options=" -reconnect 1 -reconnect_streamed 1"
" -reconnect_delay_max 5")
players[message.server.id] = player
player.start()
except Exception as e:
await client.send_message(message.server, "Error2______[Error]".format(error=e))
if not client.is_voice_connected(message.server):
try:
channel = message.author.voice.voice_channel
voice = await client.join_voice_channel(channel)
player = await voice.create_ytdl_player(yt_url, before_options=" -reconnect 1 -reconnect_streamed 1"
" -reconnect_delay_max 5")
players[message.server.id] = player
player.start()
except Exception as e:
await client.send_message(message.channel, "Error3____[error]".format(error=e))
except Exception as e:
await client.send_message(message.channel, "Error4________[error]".format(error=e))
client.run("I erased the token number before sharing, it's not the problem")
您还没有定义变量播放器。在代码的开头,添加以下内容:
玩家 = {}代码
希望这奏效了!