python - 我不知道如何解决这个问题(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
解决方案
==
用于将变量值与另一个值进行比较。
=
用于为变量赋值。
voice == vc
以上将是错误的,而不是使用,voice = vc
推荐阅读
- ios - 如何通过 Swift UI 中的按钮单击将 Textfield 值传递给视图控制器
- css - 复选框上的背景颜色不设置颜色
- java - 在网络调用后追踪 ConcurrentModificationException 的原因
- elasticsearch - 根据消息字段创建索引 - appname
- database - 从共享主机备份大型数据库。如何?
- flutter - 当我在 Flutter 的 Hive Db 中添加元素时,如何修复错误路由长度被调用为 null?
- python - 无法在自定义编码器中将 dict 编码为 JSON
- java - Spring Data JPA @Modifying 注解使用@Transactional
- android - Android Room FTS4 如何删除自动创建的触发器
- python - 使用 print(path) 时无法打印图像名称