首页 > 解决方案 > 我不知道如何解决这个问题(discord.py)

问题描述

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

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

标签: pythondiscord

解决方案


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

=用于为变量赋值。

voice == vc

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


推荐阅读