为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

开满天机 2023-10-18 15:45:10
我希望让机器人在用户加入频道时上线,并在用户离开频道时离线。我对discord.js很陌生,但这是我的代码:@client.eventasync def on_ready():    await client.change_presence(status=discord.Status.offline)    print("Bot ready")@client.eventasync 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.eventasync 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")我当然会将“Channel ID 1”和“Channel ID 2”替换为通道 ID。谢谢你的帮助!
查看完整描述

1 回答

?
眼眸繁星

TA贡献1873条经验 获得超9个赞

您有 2 个名为:“on_voice_state_update”的“def”,因此当此事件发生时,机器人不知道他必须调用哪一个。删除 2 个“def”之一,并仅在一个名为“on_voice_state_update”的“def”中执行您想要的操作。


@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")


查看完整回答
反对 回复 2023-10-18
  • 1 回答
  • 0 关注
  • 90 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信