解码器进程在尝试停止录制 pycord 时被杀死

问题描述 投票:0回答:0

我正在创建一个 discord 机器人,它使用 pycord 在语音通道中记录用户。问题是,当我在 voice_client 上调用 stop_recording 时,它会永远抛出“解码器进程被杀死”。

class VoiceSession():
    """
    A voice session with the bot
    """

    def __init__(self, bot, ctx):
        self.bot = bot
        self.ctx = ctx
        self.channel = ctx.channel
        self.member = ctx.author
        self.history = []
        self.recording = ctx.voice_client.recording
        self.chat_session = ChatGPTSession()
        self.voice_client = ctx.voice_client
        self.audio_queue = asyncio.Queue()
        self.id = random.randint(0, 1000000)
        self.recording_task = None
        self.sink = discord.sinks.MP3Sink()
        self.connections = {}

    def start_recording_task(self):
        self.recording_task = asyncio.create_task(self.recording_loop())

    def stop_recording_task(self):
        if self.recording_task is not None:
            self.recording_task.cancel()
            self.recording_task = None

    async def recording_loop(self):
        canspeakagain = True
        while True:
            if not self.recording and canspeakagain:
                await self.start_record(self.ctx)
                canspeakagain = False
            if self.recording and self.voice_client.finished_speaking:
                await asyncio.sleep(1)
                await self.stop_record(self.ctx)


    async def start(self):
        self.start_recording_task()

    async def stop(self):
        self.stop_recording_task()

    async def start_record(self, ctx):
        voice = ctx.author.voice

        vc = self.voice_client

        self.connections.update({ctx.guild.id: vc})  # Updating the cache with the guild and channel.
        
        vc.start_recording(
            discord.sinks.WaveSink(),  # The sink type to use.
            self.finished_callback,  # What to do once done.
            ctx.channel  # The channel to disconnect from.
        )
        print("started recording")
        self.recording = True

    async def finished_callback(self, sink: discord.sinks, channel: discord.TextChannel, *args):  # Our voice client already passes these in.
        print('callback called')
        recorded_users = [  # A list of recorded users
            f"<@{user_id}>"
            for user_id, audio in sink.audio_data.items()
        ]
        files = [discord.File(audio.file, f"{user_id}.{sink.encoding}") for user_id, audio in sink.audio_data.items()]  # List down the files.
        await channel.send(f"finished recording audio for: {', '.join(recorded_users)}.", files=files)  # Send a message with the accumulated files.

    async def stop_record(self, ctx):
        if ctx.guild.id in self.connections:
            vc = self.connections[ctx.guild.id]

            vc.stop_recording()
            del self.connections[ctx.guild.id]
            await ctx.delete()
        else:
            await ctx.respond("I am currently not recording here.")


控制台:

Starting...
Bot is ready.
started recording
finished speaking
finished speaking
Decoder Process Killed
Decoder Process Killed
Decoder Process Killed
Decoder Process Killed
Decoder Process Killed
Decoder Process Killed
...

我试过:

  • 用 asyncio.Task 替换 Thread 以异步运行记录循环。
  • 使用 async with 而不是 try/except 来保证语音 如果出现错误,客户端将正确断开连接。
  • 显示错误出现位置的打印语句。(在执行vc.stop_recording()之后就可以了)
discord bots recording pycord
© www.soinside.com 2019 - 2024. All rights reserved.