【发布时间】:2021-03-05 20:23:29
【问题描述】:
我正在开发一个不和谐的机器人并试图解决这个问题。我想知道如果我可以在代码的最后一行的 lambda 行中等待一个函数,我该如何在非异步函数中使用 await。
我尝试过使用 player 变量执行 asyncio.run,我也尝试过 asyncio.run_coroutine_threadsafe,但没有成功。
关于如何使它工作的任何想法?
代码:
def queue_check(self, ctx):
global queue_list
global loop
global playing
if loop is True:
queue_list.append(playing)
elif loop is False:
playing = ''
if ctx.channel.last_message.content == '$skip' or '$s':
return
song = queue_list.pop(0)
player = await YTDLSource.from_url(song, loop=self.client.loop, stream=True)
ctx.voice_client.play(player, after= queue_check(self,ctx))
@commands.command(aliases=['p'])
@commands.guild_only()
async def play(self, ctx, *, url):
global queue_list
global playing
if ctx.voice_client is None:
if ctx.author.voice:
await ctx.author.voice.channel.connect()
else:
return await ctx.send("> You are not connected to a voice channel.")
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.client.loop, stream=True)
await ctx.send(f'> :musical_note: Now playing: **{player.title}** ')
playing = url
await ctx.voice_client.play(player, after=lambda e: queue_check(self,ctx))
【问题讨论】:
-
你不能。任何使用
await的函数都必须定义为async
标签: python discord discord.py