我不知道如何解决这个问题(discord.py)

时间:2021-06-11 16:15:31

标签: python discord

这次我看了视频,然后克隆并执行了 但是如果你输入命令来播放歌曲,就会弹出一个错误 如果您能回答这个问题,我将不胜感激 (我已经尝试切换到全局变量)

import youtube_dl
import discord

intents = discord.Intents.default()
client = discord.Client(intents=intents)


@client.event
async def on_ready():
    print('켜짐')
    print(client.user.id)
    print('--------------------------')


@client.event
async def on_message(message):
    if message.content.startswith('핑크야 들어와'):
        await message.author.voice.channel.connect()
        await message.channel.send('빠밤')

    if message.content.startswith('핑크야 나가'):
        for vc in client.voice_clients:
            if vc.guild == message.guild:
                voice = vc

        await voice.disconnect()
        await message.channel.send('힝...')

    if message.content.startswith('$재생'):
        for vc in client.voice_clients:
            if vc.guild == message.guild:
                voice == vc

        url = message.content.split(" ")[1]
        option = {
            'outtmpl' : "file/" + url.split('=')[1] + ".mp3"
        }

        with youtube_dl.YoutubeDL(option) as ydl:
            ydl.download([url])
            info = ydl.extract_info(url, download=False)
            title = info["title"]

        voice.play(discord.FFmpegPCMAudio("file/" + url.split('=')[1] + ".mp3"))
        await message.channel.send(title + "재생할게!")

client.run("Tooooken")

我只写到第 47 行,你能告诉我第 343 行是否有错误吗?

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Administrator\PycharmProjects\py001\venv\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Administrator\PycharmProjects\py001\main.py", line 32, in on_message
    voice == vc
UnboundLocalError: local variable 'voice' referenced before assignment

1 个答案:

答案 0 :(得分:1)

== 用于将变量值与另一个值进行比较。

= 用于为变量赋值。

voice == vc

以上将是错误的,而是使用,voice = vc