Discord.py点歌排队系统
Discord.py song queuing system
我正在尝试编写一个 discord 音乐机器人,它一直有效,直到我将排队系统从 URL 列表转换为带有歌曲名称的字典,以便我可以显示接下来正在播放的内容。
当第二首歌曲即将播放时,我收到此错误消息:
C:\Users\sammy\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\player.py:611: RuntimeWarning: coroutine 'Command.__call__' was never awaited
self.after(error)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
这是代码,我对其进行了一些修改,使其不会太长:
global ydl_opts
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
from discord.utils import get
global voice
global queue_list
queue_list = {}
def queue():
global queue_list
print("hi")
print(len(queue_list))
if len(queue_list) != 0:
keys = list(queue_list.keys())
values = list(queue_list.values())
print("AAAAAAAAAAAAA: " + str(values[0]))
play_url(str(values[0]))
print("AAAAAAAAAAAAA2: " + str(queue_list[keys[0]]))
del queue_list[keys[0]]
print(str(queue_list))
def play_url(url):
try:
os.remove("song.mp3")
except:pass
url = str(url)
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: queue())
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.07
@client.command()
async def play(ctx, curl):
#global voice
try:
global voice
voice = await ctx.message.author.voice.channel.connect()
except:pass
if voice.is_playing() == False:
print("first")
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
return
#voice = get(client.voice_clients, guild=ctx.guild)
play_url(curl)
elif voice and voice.is_playing():
print("next")
info_dict = YoutubeDL(ydl_opts).extract_info(curl, download=False)#youtube dl attribute settings are needed
#print(info_dict['title'])
if info_dict.get('title', None) in queue_list:
queue_list[str(info_dict['title']) + str(random.randint())] = curl
else:
queue_list[str(info_dict['title'])] = curl
#print(str(queue_list))
pass
@client.command(pass_context=True)
async def pause(ctx):
#voice = get(ctx.client.voice_clients, guild=ctx.guild)
#voice = await ctx.message.author.voice.channel.connect()
if voice and voice.is_playing():
voice.pause()
@client.command(pass_context=True)
async def resume(ctx):
if voice and voice.is_paused():
voice.resume()
@client.command(pass_context=True)
async def stop(ctx):
global queue_list
queue_list = []
if voice and voice.is_playing():
voice.stop()
@client.command(pass_context=True)
async def skip(ctx):
voice.stop()
try:
os.remove("song.mp3")
except:pass
play_url(queue_list[0])
@client.command(pass_context=True)
async def queue(ctx):
values = list(queue_list.values())
keys = list(queue_list.keys())
await ctx.send("*Up Next:*")
for i in range(len(keys)):
info_dict = YoutubeDL(ydl_opts).extract_info(values[i], download=False)
message = "**" + str(i + 1) + ".** " + str(keys[i]) + " (" + str(info_dict['duration']) + ")"
await ctx.send(message)
client.run(TOKEN)
(Usually) 表示你忘记在协程(以async
开头的函数)之前使用await
,这是强制性的。
C:\Users\sammy\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\player.py:611: RuntimeWarning: coroutine 'Command.call' was never awaited
self.after(error)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
由于您的错误代码表明您忘记在第 611 行的开头添加“await”。
C:\Users\sammy\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\player.py 是你脚本的文件路径
:611: 是上述文件中的行
运行时警告:从未等待协程 'Command.call' self.after(错误) 是文件中出现的错误。
我希望这个细分对您有所帮助,有助于您的事业。
我正在尝试编写一个 discord 音乐机器人,它一直有效,直到我将排队系统从 URL 列表转换为带有歌曲名称的字典,以便我可以显示接下来正在播放的内容。
当第二首歌曲即将播放时,我收到此错误消息:
C:\Users\sammy\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\player.py:611: RuntimeWarning: coroutine 'Command.__call__' was never awaited
self.after(error)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
这是代码,我对其进行了一些修改,使其不会太长:
global ydl_opts
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
from discord.utils import get
global voice
global queue_list
queue_list = {}
def queue():
global queue_list
print("hi")
print(len(queue_list))
if len(queue_list) != 0:
keys = list(queue_list.keys())
values = list(queue_list.values())
print("AAAAAAAAAAAAA: " + str(values[0]))
play_url(str(values[0]))
print("AAAAAAAAAAAAA2: " + str(queue_list[keys[0]]))
del queue_list[keys[0]]
print(str(queue_list))
def play_url(url):
try:
os.remove("song.mp3")
except:pass
url = str(url)
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: queue())
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.07
@client.command()
async def play(ctx, curl):
#global voice
try:
global voice
voice = await ctx.message.author.voice.channel.connect()
except:pass
if voice.is_playing() == False:
print("first")
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
return
#voice = get(client.voice_clients, guild=ctx.guild)
play_url(curl)
elif voice and voice.is_playing():
print("next")
info_dict = YoutubeDL(ydl_opts).extract_info(curl, download=False)#youtube dl attribute settings are needed
#print(info_dict['title'])
if info_dict.get('title', None) in queue_list:
queue_list[str(info_dict['title']) + str(random.randint())] = curl
else:
queue_list[str(info_dict['title'])] = curl
#print(str(queue_list))
pass
@client.command(pass_context=True)
async def pause(ctx):
#voice = get(ctx.client.voice_clients, guild=ctx.guild)
#voice = await ctx.message.author.voice.channel.connect()
if voice and voice.is_playing():
voice.pause()
@client.command(pass_context=True)
async def resume(ctx):
if voice and voice.is_paused():
voice.resume()
@client.command(pass_context=True)
async def stop(ctx):
global queue_list
queue_list = []
if voice and voice.is_playing():
voice.stop()
@client.command(pass_context=True)
async def skip(ctx):
voice.stop()
try:
os.remove("song.mp3")
except:pass
play_url(queue_list[0])
@client.command(pass_context=True)
async def queue(ctx):
values = list(queue_list.values())
keys = list(queue_list.keys())
await ctx.send("*Up Next:*")
for i in range(len(keys)):
info_dict = YoutubeDL(ydl_opts).extract_info(values[i], download=False)
message = "**" + str(i + 1) + ".** " + str(keys[i]) + " (" + str(info_dict['duration']) + ")"
await ctx.send(message)
client.run(TOKEN)
(Usually) 表示你忘记在协程(以async
开头的函数)之前使用await
,这是强制性的。
C:\Users\sammy\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\player.py:611: RuntimeWarning: coroutine 'Command.call' was never awaited self.after(error) RuntimeWarning: Enable tracemalloc to get the object allocation traceback
由于您的错误代码表明您忘记在第 611 行的开头添加“await”。
C:\Users\sammy\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\player.py 是你脚本的文件路径
:611: 是上述文件中的行
运行时警告:从未等待协程 'Command.call' self.after(错误) 是文件中出现的错误。
我希望这个细分对您有所帮助,有助于您的事业。