RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1576177
Accepted
QuL1ux
QuL1ux
Asked:2024-04-15 00:43:47 +0000 UTC2024-04-15 00:43:47 +0000 UTC 2024-04-15 00:43:47 +0000 UTC

收听discord.py的语音频道

  • 772

一般来说,情况是这样的,我希望机器人进入语音通道(如果有人在那里),并收听对话,将其记录在文件中以供进一步操作。进入语音通道一切都清楚了,但是如何听声音呢?我深入研究了文档并找到了它voice_client.source,但此代码未成功打印 None:

audio_data = ctx.voice_client.source
while True:
    print(audio_data)

据我了解,您需要创建一个单独的音频流作品,但我根本不知道如何做到这一点。谁能帮忙解答一下?

python-3.x
  • 1 1 个回答
  • 67 Views

1 个回答

  • Voted
  1. Best Answer
    denisnumb
    2024-04-17T22:16:23Z2024-04-17T22:16:23Z

    您可以使用图书馆PyCord。

    pip install py-cord
    

    在那里,该类VoiceClient有一个方法start_recording,允许您录制频道中用户的声音。

    有一次,我什至遇到了记录多个用户的问题,这与接收用户声音包的实现中的一些功能有关。在我报告问题后,对实现进行了一些更改,并将一个示例直接添加到库存储库中,介绍如何使用此方法并获取录音声音文件。

    有一个例子audio_recording.py- 用于记录一个用户

    还有一个记录多个用户的例子:audio_recording_merged.py

    第二个代码:

    import io
    
    import pydub  # pip install pydub==0.25.1
    
    import discord
    from discord.sinks import MP3Sink
    
    bot = discord.Bot()
    
    
    @bot.event
    async def on_ready():
        print(f"Logged in as {bot.user}")
    
    
    async def finished_callback(sink: MP3Sink, channel: discord.TextChannel):
        mention_strs = []
        audio_segs: list[pydub.AudioSegment] = []
        files: list[discord.File] = []
    
        longest = pydub.AudioSegment.empty()
    
        for user_id, audio in sink.audio_data.items():
            mention_strs.append(f"<@{user_id}>")
    
            seg = pydub.AudioSegment.from_file(audio.file, format="mp3")
    
            # Determine the longest audio segment
            if len(seg) > len(longest):
                audio_segs.append(longest)
                longest = seg
            else:
                audio_segs.append(seg)
    
            audio.file.seek(0)
            files.append(discord.File(audio.file, filename=f"{user_id}.mp3"))
    
        for seg in audio_segs:
            longest = longest.overlay(seg)
    
        with io.BytesIO() as f:
            longest.export(f, format="mp3")
            await channel.send(
                f"Finished! Recorded audio for {', '.join(mention_strs)}.",
                files=files + [discord.File(f, filename="recording.mp3")],
            )
    
    
    @bot.command()
    async def join(ctx: discord.ApplicationContext):
        """Join the voice channel!"""
        voice = ctx.author.voice
    
        if not voice:
            return await ctx.respond("You're not in a vc right now")
    
        await voice.channel.connect()
    
        await ctx.respond("Joined!")
    
    
    @bot.command()
    async def start(ctx: discord.ApplicationContext):
        """Record the voice channel!"""
        voice = ctx.author.voice
    
        if not voice:
            return await ctx.respond("You're not in a vc right now")
    
        vc: discord.VoiceClient = ctx.voice_client
    
        if not vc:
            return await ctx.respond(
                "I'm not in a vc right now. Use `/join` to make me join!"
            )
    
        vc.start_recording(
            MP3Sink(),
            finished_callback,
            ctx.channel,
            sync_start=True,  # WARNING: This feature is very unstable and may break at any time.
        )
    
        await ctx.respond("The recording has started!")
    
    
    @bot.command()
    async def stop(ctx: discord.ApplicationContext):
        """Stop the recording"""
        vc: discord.VoiceClient = ctx.voice_client
    
        if not vc:
            return await ctx.respond("There's no recording going on right now")
    
        vc.stop_recording()
    
        await ctx.respond("The recording has stopped!")
    
    
    @bot.command()
    async def leave(ctx: discord.ApplicationContext):
        """Leave the voice channel!"""
        vc: discord.VoiceClient = ctx.voice_client
    
        if not vc:
            return await ctx.respond("I'm not in a vc right now")
    
        await vc.disconnect()
    
        await ctx.respond("Left!")
    
    
    bot.run("TOKEN")
    

    研究它是如何工作的,阅读该方法的文档start_recording,我认为,您可以根据这个示例进行某种您自己的实现。

    • 1

相关问题

  • 在 Linux 服务器上运行 Django 项目

  • 当您单击kivy设置中的关闭按钮时,如何调用更新应用程序本身的gui的方法

  • 制作一个按钮处理程序来调用该函数。那些。单击按钮时,该函数应运行。遥控机器人

  • 如何正确地将列表项添加到 Word 表格中?

  • 内容解析(Python、BeautifulSoup、请求)

  • 脚本不适用于 BeautifulSoup 和请求 (Python3x)

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5