【问题标题】:How to make a Discord bot join a voice channel and play an audio file when someone joins a voice chat如何让 Discord 机器人加入语音频道并在有人加入语音聊天时播放音频文件
【发布时间】:2020-09-05 22:48:51
【问题描述】:

我目前的项目

我正在尝试制作一个 Discord 机器人,当有人加入 Discord 语音聊天时播放特定音频文件(.mp3 或 .ogg)。

我的问题

我不知道如何做到这一点。

【问题讨论】:

  • 我不确定它是否总是可以连接到一个频道,至少就我所经历的而言
  • 嗯,没错。但是当其他成员加入时,我可以在服务器中加入某个语音聊天吗?

标签: python-3.x audio discord.py-rewrite


【解决方案1】:

我认为这应该可行。您必须安装 ffmpeg 并安装所需的模块,但如果您有任何问题,请告诉我。

import discord
import audioread
import time

@client.event
async def on_voice_state_update(member: discord.Member, before, after):
#replace this with the path to your audio file
    path = r"/path/to/file.mp3"

    vc_before = before.channel
    vc_after = after.channel
    if vc_before == vc_after:
        return
    if vc_before is None:
        channel = member.voice.channel
        vc = await channel.connect()
        sleep(.5)
        vc.play(discord.FFmpegPCMAudio(path))
        with audioread.audio_open(path) as f:
            #Start Playing
            sleep(f.duration)
        await vc.disconnect()

    elif vc_after is None:
        return
    else:
        channel = member.voice.channel
        vc = await channel.connect()
        sleep(.5)
        vc.play(discord.FFmpegPCMAudio(path))
        with audioread.audio_open(path) as f:
            #Start Playing
            sleep(f.duration)
        await vc.disconnect()

【讨论】:

  • 我试过这段代码。我把它和其他代码放在一起,让机器人上线,但它不会加入语音聊天。语音聊天的 ID 是 663452123299840017,如果有帮助的话。到目前为止,这是我的代码:Code--agentlonestar.repl.co
  • 顺便说一句,感谢您到目前为止的帮助。
  • 并且只有在用户加入语音频道时才会加入语音频道
  • 控制台没有错误,实际上甚至没有任何控制台输出。至于加入语音聊天,当我运行 .py 文件时,机器人会上线,但它根本不加入语音聊天。我尝试了服务器中的所有语音聊天,但从未加入。
  • 您能用您的代码制作一个 pastebin 以便我查看吗?
猜你喜欢
  • 2019-04-07
  • 2022-01-04
  • 2020-07-16
  • 1970-01-01
  • 2020-08-30
  • 2021-07-13
  • 2020-09-26
  • 2023-02-25
  • 2019-06-18
相关资源
最近更新 更多