I built a Discord bot in Python that should join voice channels and play random audio files when users type a specific command. The bot works perfectly the first time someone uses the command, but then it completely stops responding to that same command afterwards.
The bot is supposed to join the user’s voice channel, pick a random audio file from a directory, play it, and then leave the channel automatically. I can still use other commands to manually summon the bot, but the main audio command becomes unresponsive.
def disconnect_bot(self, ctx):
current_state = self.get_voice_state(ctx.message.server)
disconnect_task = current_state.voice.disconnect()
future_task = asyncio.run_coroutine_threadsafe(disconnect_task, current_state.voice.loop)
try:
future_task.result()
except:
pass
@commands.command(pass_context=True, no_pm=True)
async def playaudio(self, ctx):
current_state = self.get_voice_state(ctx.message.server)
settings = {
'default_search': 'auto',
'quiet': True,
}
if current_state.voice is None:
join_success = await ctx.invoke(self.summon)
if not join_success:
return
try:
audio_file = audio_folder + "\\" + random.choice(os.listdir(audio_folder))
audio_player = current_state.voice.create_ffmpeg_player(audio_file, after=lambda: self.disconnect_bot(ctx))
audio_player.start()
except Exception as error:
error_msg = 'Something went wrong: ```py\n{}: {}\n```'
await self.bot.send_message(ctx.message.channel, error_msg.format(type(error).__name__, error))
What could be causing this one-time execution issue? Is there something wrong with how I’m handling the voice connection or the cleanup process?