python - 如何从python中的非异步方法发布嵌入
问题描述
我想在调用函数“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 ;)")
解决方案
你可以这样做
async def play_next(self, ctx):
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)
await self.voicechannel.play(discord.FFmpegPCMAudio(source=source), after=lambda e: self.play_next(ctx))
else:
await self.voicechannel.disconnect()
注意:异步可以包含在任何定义中。或者你可以这样做
@commands.command() #If its on cog, if its not on cog change it to @client.command or @bot.command()
async def play_next(self, ctx):
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)
await self.voicechannel.play(discord.FFmpegPCMAudio(source=source), after=lambda e: self.play_next(ctx))
else:
await self.voicechannel.disconnect()
或删除@commands.command()
并制作另一个这样的命令
@commands.command()
async def play(self, ctx, *, url):
await play_next(url)
"""Say something or add another code"""
推荐阅读
- python - 用新的数据框更新数据框,覆盖
- button - 如何根据页面语言更改按钮位置?
- r - 使用 ggplot 按百分比显示直方图
- python - 如何让使用正则表达式匹配年份和月份?
- angular - 如何销毁以编程方式注入动态创建的角度组件的服务?
- css - Tailwind CSS:溢出隐藏不隐藏完整翻译
- openshift - AWS 上的 Openshift 自动允许以 root 用户身份部署容器
- python - sklearn 中的 ccp_alpha 查找 optmila 值
- sql-server - 需要 Azure sql 专用终结点 ARM 模板帮助
- python - 如何将 Django 表单中的数据作为列表输入?