gpt4 book ai didi

python-3.x - 如何让 Discord 机器人加入语音 channel 并在有人加入语音聊天时播放音频文件

转载 作者:行者123 更新时间:2023-12-03 01:18:05 24 4
gpt4 key购买 nike

我目前的项目
当有人加入 Discord 语音聊天时,我正在尝试制作一个播放特定音频文件(.mp3 或 .ogg)的 Discord 机器人。
我的问题
我不知道如何做到这一点。

最佳答案

我认为这应该有效。您必须安装 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()

关于python-3.x - 如何让 Discord 机器人加入语音 channel 并在有人加入语音聊天时播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61898377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com