Discord py 在从 voice.play(... after=...) 的 after 方法调用时不发送消息

问题描述 投票:0回答:1
from discord.ext import commands
from discord import FFmpegPCMAudio
from asyncio import run

FFMPEG_OPTIONS = {'before_options': '-avioflags direct', 'options': '-vn -sn -dn -movflags +faststart -b:a 49k'}
class Play(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.run = False
        self.voice = ""

    async def play_song(self, ctx ,data=()):
        if data != ():
            data = "+".join(data)
            print(data)
        else:
            print("Passes Automatically")
            await ctx.send("auto pass")

        await ctx.send("playing")

        source = FFmpegPCMAudio("https://www2.iis.fraunhofer.de/AAC/ChID-BLITS-EBU-Narration.mp4", **FFMPEG_OPTIONS)
        await self.voice.play(source, after=lambda x=None: run(self.play_song(ctx)))

    @commands.Cog.listener()
    async def on_ready(self):
        print("Play is working")


    @commands.command(pass_context=True)
    async def play(self, ctx, *initial_user_input):
        if not self.run:
            if ctx.author.voice:
                channel = ctx.message.author.voice.channel
                self.voice = await channel.connect()
                self.run = True
                await self.play_song(ctx, initial_user_input)
            else:
                await ctx.send("Channel Not Found")
        else:
            await self.play_song(ctx, initial_user_input)


async def setup(bot):
    await bot.add_cog(Play(bot))

上面的代码是我正在使用的,当从播放命令调用 play_song 时,它工作正常(消息发送等)。当从 lambda 函数调用 play_song 方法时。它按预期工作,直到到达await ctx.send(“testing”)。显然它应该在不和谐频道中发送消息,但它无限期地挂起。

我尝试从 lambda 函数调用 play 函数(同样的问题)。我尝试传递通道 ID 而不是上下文,但都不起作用。我在没有等待的情况下运行了它,此时它只是说从未等待过。我还使用异步生成器对其进行了测试,以防它是 asyncio.run 但同样的问题。感谢帮助。

python asynchronous async-await discord discord.py
1个回答
0
投票

可能是因为运行歌曲就像机器人正在执行的主要任务,所以在完成之前它不能做任何其他事情 所以不要使用 asyncio.run 使用 self.bot.loop.create_task

我建议添加 Song_finished 方法,以便您知道它何时完成有助于整体调试,并且您可以随时更改内容 这是修改后的代码

from discord.ext import commands
from discord import FFmpegPCMAudio
import asyncio

FFMPEG_OPTIONS = {'before_options': '-avioflags direct', 'options': '-vn -sn -dn -movflags +faststart -b:a 49k'}
class Play(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.run = False
        self.voice = None

    async def play_song(self, ctx, data=()):
        if data:
            data = "+".join(data)
            print(data)
        else:
            print("Passes Automatically")
            await ctx.send("auto pass")

        await ctx.send("playing")

        source = FFmpegPCMAudio("https://www2.iis.fraunhofer.de/AAC/ChIDBLITS-EBU-Narration.mp4", **FFMPEG_OPTIONS)
        self.voice.play(source, after=lambda e:self.bot.loop.create_task(self.song_finished(ctx)))

    async def song_finished(self, ctx):
        await ctx.send("Song finished playing")
        # Optionally, you can call play_song again if you have more songs to play

    @commands.Cog.listener()
    async def on_ready(self):
        print("Play is working")

    @commands.command(pass_context=True)
    async def play(self, ctx, *initial_user_input):
        if not self.run:
            if ctx.author.voice:
                channel = ctx.message.author.voice.channel
                self.voice = await channel.connect()
                self.run = True
                await self.play_song(ctx, initial_user_input)
            else:
                await ctx.send("Channel Not Found")
        else:
            await self.play_song(ctx, initial_user_input)
async def setup(bot):
    await bot.add_cog(Play(bot))
© www.soinside.com 2019 - 2024. All rights reserved.