如何在python中从非异步方法发布嵌入

2024-04-29 11:43:15 发布

您现在位置: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 ;)")

Tags: pathselfurlsourceplayifqueuesong
2条回答

你可以这么做

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()

注意:异步可以包含在任何def中。 或者你可以这样做

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

在discord机器人中使用同步功能是一个非常糟糕的主意,它可能会冻结整个机器人。在您的情况下,我建议将play_next函数更改为async

相关问题 更多 >