如何让 Discord 机器人加入语音频道并在有人加入语音聊天时播放音频文件
How to make a Discord bot join a voice channel and play an audio file when someone joins a voice chat
我当前的项目
我正在尝试制作一个 Discord 机器人,当有人加入 Discord 语音聊天时它会播放特定的音频文件(.mp3 或 .ogg)。
我的问题
我不知道如何做到这一点。
我认为这应该可行。您必须安装 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()
我当前的项目
我正在尝试制作一个 Discord 机器人,当有人加入 Discord 语音聊天时它会播放特定的音频文件(.mp3 或 .ogg)。
我的问题
我不知道如何做到这一点。
我认为这应该可行。您必须安装 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()