我最近正在创建一个 Discord 机器人,并想创建一个 /effectoom 命令。运行命令后,客户端加入频道并应播放代码中指定的源。
我不知道为什么,但它可以连接但不播放任何内容。这是输出:
[2024-07-11 14:27:51] [INFO ] discord.voice_client: Connecting to voice... [2024-07-11 14:27:51] [INFO ] discord.voice_client: Starting voice handshake... (connection attempt 1)
这是代码:(我使用 from discord import *)
` 类效果(app_commands.Group):
def __init__(self):
super().__init__(
name="effect",
description="Creates chaos in your VC"
)
@self.command(name="boom", description="Plays a boom sound in your voice channel")
@app_commands.guild_only()
async def effect_boom(inc: Interaction):
if not inc.guild:
await inc.response.send_message(
embed=Embed(
description="You must be in a guild to perform this command! <a:amongus:1255247106252214276>",
color=0xFCFCED
),
ephemeral=True
)
return
if not inc.user.voice:
await inc.response.send_message(
embed=Embed(
description="You must be in a voice channel to perform this command! <a:amongus:1255247106252214276>",
color=0xFCFCED
),
ephemeral=True
)
return
if inc.guild.me.voice:
await inc.response.send_message(
embed=Embed(
description="I'm currently in a voice channel, you're in queue...",
color=0xFCFCED
),
ephemeral=True
)
while inc.guild.me.voice:
...
source = PCMVolumeTransformer(FFmpegPCMAudio("bobo/audio/boom.mp3"))
await voice_client.VoiceClient(inc.client, inc.user.voice.channel).connect(reconnect=False, timeout=None, self_deaf=True)
await voice_client.VoiceClient(inc.client, inc.user.voice.channel).play(source, after=(await voice_client.VoiceClient(inc.client, inc.user.voice.channel).disconnect(force=True)))
await inc.edit_original_response(
embed=Embed(
description="The boom effect has been played!",
color=0xFCFCED
)
)
return
while inc.guild.me.voice:
...
source = PCMVolumeTransformer(FFmpegPCMAudio("bobo/audio/boom.mp3"))
await voice_client.VoiceClient(inc.client, inc.user.voice.channel).connect(reconnect=False, timeout=None, self_deaf=True)
await voice_client.VoiceClient(inc.client, inc.user.voice.channel).play(source, after=(await voice_client.VoiceClient(inc.client, inc.user.voice.channel).disconnect(force=True)))
await inc.response.send_message(
embed=Embed(
description="The boom effect has been played!",
color=0xFCFCED
)
)
`
任何形式的帮助表示赞赏!
您应该尝试执行此操作以在 VoiceChannel 中播放音频源:
voiceChannelConnect = await voiceChannel.connect()
audioSource = discord.FFmpegPCMAudio(executable="C:\\Path\\To\\ffmpeg.exe", source="sound.mp3")
voiceChannelConnect.play(audioSource)