Search code examples
pythondiscord

Python discord bot not joining voice channel, but without sending an error


I made a music bot for a discord server one month ago, using discord.py. It worked well until one week ago, but now when I use the join command the bot fails to connect to the voice channel. Here is the code of the command :

class MonClient(discord.Client):
      
   async def on_ready(self):
      self.voice = None # contains the <VoiceClient> object created by <chan.connect> in the join command

   async def on_message(self,message):
      if message.author.bot:
         pass
      elif message.content[0] == "!": # the command prefix is "!"
         parts = message.content.split(" ")
         command = parts[0][1:]
         
         if command == "join":
            if message.author.voice != None and self.voice == None:
               chan = message.author.voice.channel
               await message.channel.send("before connecting to voice channel")
               self.voice = await chan.connect()
               await message.channel.send("after connecting to voice channel")
            else:
               await message.channel.send("Bot already connected to voice channel or user not connected to voice channel")

More precisely, the bot successfully sends "before connecting to voice channel" in the text channel, then it is shown to have joined to the voice channel by the Discord UI (its image is shown in the list of members connected to the voice channel, there is the sound of connection to the voice channel). But the second message "after connecting to voice channel" is never sent. We can still use the bot's other commands, but the variable self.voice still contains None, so obviously the bot can't play music.

I suppose the asynchronous connect method stops on something indefinitely, but I can't understand what, because there is no error message, not even the TimeoutError described by the documentation. The internet connection seems fine, and as I said the bot worked well for two or three weeks, so now I really don't know how to better understand what blocks the connection.

There are some other questions on Stackoverflow for similar issues, but the proposed answers had no relation with my problem : an answer proposed another method to get the voice channel object to connect to, but I already get the good object since the bot is at least shown to connect to the voice channel ; and another answer had to do with the @client.command() lines, which I don't use.


Solution

  • Make sure your library is up to date, discord made a change recently. It worked for me.

    pip install -U discord