我想在调用函数“play_next()”时发布嵌入。问题在于此方法是非异步方法。有人能给我解释一下如何从非异步方法进行异步调用吗
def play_next(self, ctx):
self.voicechannel = discord.utils.get(self.bot.voice_clients, guild=ctx.guild)
if len(self.song_queue) >= 1:
del self.song_queue[0]
source = os.path.join(self.QUEUE_PATH, self.song_queue[0])
audio = MP3(source)
await self.postEmbed(ctx, self.song_queue[0], audio.info.length)
self.voicechannel.play(discord.FFmpegPCMAudio(source=source), after=lambda e: self.play_next(ctx))
else:
self.voicechannel.disconnect()
编辑:
@commands.command()
async def play(self, ctx, url):
connect_check = await self.join(ctx)
if connect_check:
video_title = self.getUrlTitle(url) + ".mp3"
check_path = os.path.join(self.QUEUE_PATH, video_title)
song = ""
print(os.path.exists(check_path))
if not os.path.exists(check_path):
print("We are in IF")
url_song = await self.download_song(url)
song = os.path.join(self.QUEUE_PATH, url_song)
else:
print("We are in Else")
song = check_path
self.song_queue.append(song)
if not self.voicechannel.is_playing():
audio = MP3(song)
await self.postEmbed(ctx, self.song_queue[0], audio.info.length)
await self.voicechannel.play(discord.FFmpegPCMAudio(source=song), after=lambda e: self.play_next(ctx))
self.voicechannel.volume = 100
await ctx.send('Now playing...')
else:
await ctx.send('Song queued')
else:
await ctx.send("Der Bot ist bereits in einem Channel! Er kann nicht an zwei Orten gleichzeitig sein ;)")
你可以这么做
注意:异步可以包含在任何def中。 或者你可以这样做
或者删除
@commands.command()
并发出另一个类似这样的命令在discord机器人中使用同步功能是一个非常糟糕的主意,它可能会冻结整个机器人。在您的情况下,我建议将
play_next
函数更改为async相关问题 更多 >
编程相关推荐