Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord Bot won't tell someone to join a voice channel, instead it just throws an error. Other parts work totally fine

I’ve been working on this function for my Discord bot for quite a while and finally got most of it up and running, but I am running into an issue for my "Connect to a voice channel" warning. The segment of code is as follows:

 @commands.command(name="play", help="Plays a selected song from youtube")
    async def p(self, ctx, *args):
        query = " ".join(args)
        
        voice_channel = ctx.message.author.voice.channel
        if voice_channel is None:
            #you need to be connected so that the bot knows where to go
            await message.channel.send("Connect to a voice channel!")
        else:
            song = self.search_yt(query)
            if type(song) == type(True):
                await ctx.send("Could not download the song. Incorrect format try another keyword. This could be due to playlist or a livestream format.")
            else:
                await ctx.send("Song added to the queue")
                self.music_queue.append([song, voice_channel])

Now this all seems to be working pretty well except for the "if voice_channel is None" portion. An error keeps getting thrown saying "NoneType’ object has no attribute ‘channel’".

I’m sure this is fairly simple and I understand the gist of the problem (obviously not the first time I’ve seen ‘NoneType’ object errors, but I just can’t seem to figure out what I need to change here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As I said previously, As long as I am in a voice channel everything works perfectly. The bot connects, downloads, & plays the music.

>Solution :

You need to use try: like so:

@commands.command(name="play", help="Plays a selected song from youtube")
    async def p(self, ctx, *args):
        query = " ".join(args)
        try:
            voice_channel = ctx.message.author.voice.channel
        except:
            await message.channel.send("Connect to a voice channel!")
        song = self.search_yt(query)
        if type(song) == type(True):
            await ctx.send("Could not download the song. Incorrect format try another keyword. This could be due to playlist or a livestream format.")
        else:
            await ctx.send("Song added to the queue")
            self.music_queue.append([song, voice_channel])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading