为什么我的 discord.py 事件没有被调用?

Why are my discord.py events not being called?

我希望让机器人在用户加入频道时上线,在用户离开频道时下线。我是 discord.js 的新手,但这是我的代码:

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.offline)
    print("Bot ready")

@client.event
async def on_voice_state_update(member, before, after):
    if before.channel is None and after.channel is not None:
        if after.channel.id == [""Channel ID 1""] or after.channel.id == [""Channel ID 2""]:
            await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name="users | Ready to mute"))
            print("Bot done0")
            print(member, "joined")

@client.event
async def on_voice_state_update(member, before, after):
    if before.channel is not None and after.channel is None:
        if before.channel.id == [""Channel ID 1""] or before.channel.id == [""Channel ID 2""]:
            await client.change_presence(status=discord.Status.offline)
            print("Bot done1")
            print(member, "left")

我当然会用频道 ID 替换“频道 ID 1”和“频道 ID 2”。

感谢您的帮助!

你有 2 个 'def' 命名为:“on_voice_state_update”,所以当这个事件发生时,机器人不知道他必须调用哪个。删除 2 个 'def' 中的一个,只在一个 'def' 中做你想做的事:“on_voice_state_update”。

@client.event
async def on_voice_state_update(member, before, after):
    if before.channel is None and after.channel is not None:
        if after.channel.id == [""Channel ID 1""] or after.channel.id == [""Channel ID 2""]:
            await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name="users | Ready to mute"))
            print("Bot done0")
            print(member, "joined")
    elif before.channel is not None and after.channel is None:
            if before.channel.id == [""Channel ID 1""] or before.channel.id == [""Channel ID 2""]:
                await client.change_presence(status=discord.Status.offline)
                print("Bot done1")
                print(member, "left")