Wednesday 24 May 2023

Discord.py bot joins voice channel but when using voicechannel.play i get error:Command raised an exception: ClientException: Not connected to voice

So i've managed to get my discord bot to join a voice channel but when i use the play command it gives me an error that its not conencted to voice Command raised an exception: ClientException: Not connected to voice.

Here is my code:

import discord
import random
import glob
from discord.ext import commands

##discord intents 
intents = discord.Intents()
intents.members = True
intents.messages = True
intents.guilds = True
intents.voice_states = True

connect the bot to a voice channel:

    
##called when user wants bot to join voice channel
@bot.command(name ='join', help = 'Make the bot join a voice channel')
async def join(context):
    
    botVoice = context.message.guild.voice_client
    if context.guild.voice_client:
        botvoicechannel = context.message.guild.voice_client.channel
    else:
        botvoicechannel = None
    authorVoice = context.author.voice
    if context.author.voice:
        authorvoicechannel = context.author.voice.channel
    else:
        authorvoicechannel = None
    
    ##await context.reply('bot voice channel: {}\n\nbot voice:\n{}\n\nauthor voice channel: {}\n\nauthor voice voice:\n{}\n\n'.format(botvoicechannel, botVoice, authorvoicechannel, authorVoice))
    
    if not authorVoice and not botVoice:
        await context.reply('Connect to a voice channel first.')
        return
    elif authorVoice and not botVoice:
        await context.reply('Connecting to {}'.format(authorvoicechannel))
        await authorvoicechannel.connect()
        return
    elif not authorVoice and botVoice:
        await context.reply("You aren't in a channel, I'm in {}".format(botvoicechannel))
        return
    elif authorVoice and botVoice:
        if (botvoicechannel == authorvoicechannel):
            await context.reply("I'm already in here.")
            return
        else:
            await context.reply('Moving to you!')
            await botVoice.move_to(authorvoicechannel)
            return
        return

and have it play a url:

@bot.command(name ='play', help = 'Make the bot play a url')
async def play(context, url):
    botVoice = context.message.guild.voice_client
    audioSource = discord.FFmpegPCMAudio(url, executable="ffmpeg")
    botVoice.play(audioSource, after = None)

the code works and the bot joins the voice channel. i see it in the voice channel with me, however i get an error when it gets to botVoice.play(audioSource, after = None)

the error message is: error: Command raised an exception: ClientException: Not connected to voice.

i changed botVoice = context.message.guild.voice_client from botVoice = context.guild.voice_client but that didnt seem to change anything. not sure what to try next. It seems like it wants to play the url the bot just doesnt realize its in the voice channel with me.

maybe a related error. if i kill my python script the bot remains in the channel even though its not running. then when i start it up and do the !join command it says its joining even though its already in the channel with me. its weird because on the join command it checks to see if its already in a voice channel so it should know that its in there with me. idk what to try next. thanks for any suggestions. i only posted relevant code. let me know if you think im missing something else.

thanks for the help



from Discord.py bot joins voice channel but when using voicechannel.play i get error:Command raised an exception: ClientException: Not connected to voice

No comments:

Post a Comment