Discord Python Music bot错误

我最近开始通过为Discord制作机器人来学习python . 现在我正在制作音乐机器人,但是我收到了一个错误,无法找到错误的位置 . 你能帮忙吗?它加入了 Channels ,但在Chat中提供了Error3,如果我写了!再次播放(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")

回答(1)

2 years ago

您还没有定义变量播放器 . 在代码的开头,添加以下内容:

players = {}

希望这有效!