on_voice_state_update() 在 [vc -> vc] 时不像预期的那样 运行

on_voice_state_update() not running as expected when going [vc -> vc]

以下代码用于根据正在玩的游戏更改语音通道的名称。当状态变化为 [NONE -> vc] & [vc -> None] 时效果很好,但在 vc -> vc.

时效果不佳
@client.event
async def on_voice_state_update(member, before, after):
    
    channel = before.channel
    print("debug 1")
    try:
        print("debug 2")
        if len(channel.members) == 0:
            print("debug 3")
            if str(channel.name).startswith("Currently"):
                print("debug 4")
                await channel.edit(user_limit=0, name="|Dormant", overwrites=discord.PermissionOverwrite(read_messages = False))
                await asyncio.sleep(600)
                await channel.edit(name="Waiting for game to be played...", roles=None, reason="The voice channel was empty")
                await channel.set_permissions(role=discord.Role.name())
        else:
            print("debug 5")
    except Exception:
        print("Leaving phase was not called")
        
    try:
        channel = after.channel
        print(f"There was an update in id: {channel.id}")
        if channel.name == "Waiting for game to be played...":
            print(f"user {member.name} joined")
            new_name = f"Currently playing: {discord.utils.get(member.activities, type=discord.ActivityType.playing)}"
            member_names = channel.members
            await channel.edit(name=new_name, roles=None, reason=f"{member_names[0]} started playing {new_name}")
    except Exception:
        print("Joining phase was not called")

这是为什么,我该如何解决?

预期的行为是,如果旧频道的名称为空,则将切换为“|休眠”,否则将不进行任何更改。新频道的名称也应切换为该频道中正在播放的任何游戏,请参阅:await channel.edit(name=new_name, roles=None, reason=f"{member_names[0]} started playing {new_name}")

编辑:

我对代码做了一些更改,现在它在进入 [vc -> vc] 时运行加入阶段,但在任何情况下都不会运行离开阶段 ([vc -> vc], [vc -> None])

也许一开始你应该做。这会检查您的操作何时生效。

if before.channel is None and after.channel is not None:
   ## do your stuff here
   
elif before.channel is not None and after.channel is None:
   ## do your stuff here
   

我用这个来记录和监控 he/she 加入我服务器频道的人。