我正在尝试编写一些代码,当有人获得或失去角色时,它会向设定的频道发送消息

I'm trying to code something that when someone gets or loses a role it sends a message to a set channel

我试图让机器人在每次有人收到并失去设定频道中的黑暗角色和光明角色时发送消息,但由于某种原因,一旦我添加了黑暗氏族离开和加入消息就停止工作光族一号。我对 discord.py 有点陌生,但仍然知道很多事情。

这是我的代码:

@client.event
async def on_member_update(before, after):
    role = discord.utils.get(before.guild.roles, name='Dark Clan')
    channel = discord.utils.get(before.guild.channels, name='-dark-clan')
    if role not in before.roles and role in after.roles:
        await channel.send(f"Welcome to the Dark Clan {before.mention}!")
    if role in before.roles and role not in after.roles:
        await channel.send(f"{before.mention} left the clan.")

@client.event
async def on_member_update(before, after):
    roled = discord.utils.get(before.guild.roles, name='Light Clan')
    channeld = discord.utils.get(before.guild.channels, name='⚪-light-clan')
    if roled not in before.roles and roled in after.roles:
        await channeld.send(f"Welcome to the Light Clan {before.mention}!")
    if roled in before.roles and roled not in after.roles:
        await channeld.send(f"{before.mention} left the clan.")```

你应该为此使用 on_member_update() 事件。

@bot.event
async def on_member_update(before, after):
    role = discord.utils.get(before.guild.roles, name='Dark Clan')
    channel = discord.utils.get(before.guild.channels, name='-dark-clan')
    if role not in before.roles and role in after.roles:
        await channel.send(f"Welcome to the Dark Clan {before.mention}")
    if role in before.roles and role not in after.roles:
        await channel.send(f"{before.mention} left the Dark Clan")

我通过结合黑暗和光明氏族脚本修复了它。

@client.event
async def on_member_update(before, after):
    role = discord.utils.get(before.guild.roles, name='Dark Clan')
    channel = discord.utils.get(before.guild.channels, name='-dark-clan')
    if role not in before.roles and role in after.roles:
        await channel.send(f"Welcome to the Dark Clan {before.mention}!")
    if role in before.roles and role not in after.roles:
        await channel.send(f"{before.mention} left the clan.")
    roled = discord.utils.get(before.guild.roles, name='Light Clan')
    channeld = discord.utils.get(before.guild.channels, name='⚪-light-clan')
    if roled not in before.roles and roled in after.roles:
        await channeld.send(f"Welcome to the Light Clan {before.mention}!")
    if roled in before.roles and roled not in after.roles:
        await channeld.send(f"{before.mention} left the clan.")