首页 > 解决方案 > 如何从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 ;)")

标签: pythonpython-3.xdiscorddiscord.pydiscord.py-rewrite

解决方案


你可以这样做

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"""

推荐阅读