Search code examples
discord.pypycord

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


I'm trying to have the bot send a message every time someone receives and loses the Dark Role and Light Role in a set channel but for some reason, the dark clan leave and join message stops working once I add the light clan one. I'm somewhat new to discord.py but still, know quite a few things.

This is my code:

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

Solution

  • I fixed it by combining both the dark and light clan script.

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