如何用Python实现音乐Bot自动成为舞台频道发言者?已尝试方案无效
如何用Python让Discord音乐Bot自动成为舞台频道发言者
我需要制作一个音乐Bot,让它能自动成为Discord舞台频道的发言者,用Python实现。请问该怎么操作?
我最初的连接并播放音乐的代码如下:
voice_channel = self.bot.get_channel("ChanelID") if voice_channel is not None: self.player = await voice_channel.connect(self_deaf=False, reconnect=True) self.player.play(FFmpegPCMAudio(executable=FFMPEG_LOCATION, source=RADIO_STREAM)) logger.info("The bot has connected to the voice channel and started playing music.")
我尝试了两种解决方案,但都没效果:
第一种尝试:
if voice_channel is not None: self.player = await voice_channel.connect(self_deaf=False, reconnect=True) self.player.play(FFmpegPCMAudio(executable=FFMPEG_LOCATION, source=RADIO_STREAM)) await self.stage_instance.edit(speaker=True) logger.info("Connecting to the voice channel and playing music.")
第二种尝试:
await voice_channel.request_to_speak() await voice_channel.edit(suppress=False)
正确实现步骤
- 确认频道类型:首先要确保获取的
voice_channel是舞台频道(StageChannel类型),普通语音频道没有发言者机制。 - 获取StageInstance对象:不能直接使用未初始化的
self.stage_instance,需通过voice_channel.stage_instance获取当前频道的舞台实例。 - 检查Bot权限:确保Bot拥有
MANAGE_CHANNELS权限,这是修改舞台发言者状态的必要条件。 - 设置发言状态:连接到频道后,通过
StageInstance的edit方法直接设置发言者,或用request_to_speak请求发言后解除自身静音。
修正后的示例代码:
import discord voice_channel = self.bot.get_channel("ChannelID") # 确认是舞台频道 if isinstance(voice_channel, discord.StageChannel): # 连接到舞台频道 self.player = await voice_channel.connect(self_deaf=False, reconnect=True) self.player.play(FFmpegPCMAudio(executable=FFMPEG_LOCATION, source=RADIO_STREAM)) # 获取当前舞台实例 stage_instance = voice_channel.stage_instance if stage_instance: # 将Bot设为发言者 await stage_instance.edit(speaker=True) # 若Bot无直接发言权限,可先请求发言再解除静音 # await voice_channel.request_to_speak() # await self.bot.guilds[0].get_member(self.bot.user.id).edit(suppress=False) logger.info("Bot已连接舞台频道并开始播放音乐,已设置为发言者")
内容的提问来源于stack exchange,提问作者Beenland
相关产品推荐
相关产品推荐

