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

huangapple go评论49阅读模式
英文:

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

问题

我一个月前为一个Discord服务器制作了一个音乐机器人,使用了discord.py。直到一周前,它都运行良好,但现在当我使用 join 命令时,机器人无法连接到语音频道。以下是该命令的代码:

class MonClient(discord.Client):
      
   async def on_ready(self):
      self.voice = None # 包含由join命令中的chan.connect创建的<VoiceClient>对象

   async def on_message(self,message):
      if message.author.bot:
         pass
      elif message.content[0] == "!": # 命令前缀是 "!"
         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("连接到语音频道前")
               self.voice = await chan.connect()
               await message.channel.send("连接到语音频道后")
            else:
               await message.channel.send("机器人已连接到语音频道或用户未连接到语音频道")

更具体地说,机器人成功地在文本频道中发送了 "连接到语音频道前",然后在Discord UI中显示已加入语音频道(在连接到语音频道的成员列表中显示其图像,有连接到语音频道的声音)。但第二条消息 "连接到语音频道后" 从未发送。我们仍然可以使用机器人的其他命令,但变量 self.voice 仍然包含 None,所以显然机器人无法播放音乐。

我想异步的 connect 方法在某个地方陷入了无限期的停止,但我无法理解是什么原因,因为没有错误消息,甚至没有文档中描述的TimeoutError。网络连接似乎正常,正如我所说,机器人运行良好了两三周,所以我真的不知道如何更好地理解是什么阻止了连接。

在Stackoverflow上有一些类似问题的其他问题,但提出的答案与我的问题无关:一个答案提出了另一种获取要连接的语音频道对象的方法,但我已经获得了正确的对象,因为机器人至少显示已连接到语音频道;另一个答案与 @client.command() 行有关,但我没有使用它们。

英文:

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 &lt;VoiceClient&gt; object created by &lt;chan.connect&gt; in the join command

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

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.

答案1

得分: 1

确保你的库是最新的,discord 最近进行了更改。这对我有用。

pip install -U discord

英文:

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

pip install -U discord

huangapple
  • 本文由 发表于 2023年3月31日 02:22:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定