简体   繁体   English

Discord Api 机器人未始终加入语音频道

[英]Discord Api bot not joining the voice channel consistently

I have made a bot that joins the voice channel whenever a discord member joins said channel,but sometimes when the member leaves and joins back i get the error that the bot is already connected,so i added the is_connected function to check if the bot is already conected before attempting to join.For some reason this fuction works only half the time,if i join and leave the call mutiple times eventually it just stops joining and it leaves no error code,it just stops.我已经制作了一个机器人,它可以在不和谐成员加入所述频道时加入语音频道,但有时当成员离开并重新加入时,我会收到机器人已经连接的错误,所以我添加了 is_connected 函数来检查机器人是否是在尝试加入之前已经连接。由于某种原因,此功能仅在一半时间内有效,如果我多次加入并离开通话,最终它会停止加入并且不会留下错误代码,它会停止。 This is the code:这是代码:

@client.event
async def on_ready():
  
  print("Logged in as {0.user}".format(client))

@client.event
async def on_voice_state_update(member, before, after):

  if not before.channel and after.channel and member.id == 450333776485285919 or member.id==232855365082021890 or member.id==282234566066962433 or member.id==256759154524291074:
    
    channel = client.get_channel(988050675604803648)
    
    await is_connected()
    
    
  

@client.event
async def  is_connected():
    channel = client.get_channel(988050675604803648)
    voice_client=discord.utils.get(client.voice_clients)
    
    if(voice_client==None):
      
      await channel.connect()
    else:
      print("is connected:")

Did i use the function wrong?我是不是用错了功能?

you created an entire function for is_connected() which isn't entirely necessary.您为 is_connected() 创建了一个并非完全必要的完整函数。 You need to use that event within the on_voice_state_update to check if the bot is already connected to that current voice channel.您需要在 on_voice_state_update 中使用该事件来检查机器人是否已连接到该当前语音通道。

@client.event
async def on_voice_state_update(member, before, after):

  if not before.channel and after.channel and member.id == 450333776485285919 or member.id==232855365082021890 or member.id==282234566066962433 or member.id==256759154524291074:
    
channel = client.get_channel(988050675604803648)

if client.voice.is_connected():
  return print("already connected")
    
await channel.connect()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM