PINK_ POMA
PINK_ POMA

Reputation: 5

I don't know how to fix this(discord.py)

I watched the video this time and cloned it and executed it But if you type in the command to play the song, an error pops up I'd really appreciate it if you could answer this (I've already tried switching to a global variable)

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")

I only wrote up to the 47th line, can you tell me if there was an error on the 343rd line?

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

Upvotes: 0

Views: 49

Answers (1)

Buddy Bob
Buddy Bob

Reputation: 5889

== is for comparing a variables value to another.

= is for assigning a value to a variable.

voice == vc

The above would be wrong instead use, voice = vc

Upvotes: 1

Related Questions