#On_member_update not triggering from role-changes in "Channels & Roles"

1 messages · Page 1 of 1 (latest)

verbal ore
#

Hi guys, this code is triggering for manual role alterations, but not for role changes triggered by the channels and roles community feature.

Can someone point me in the right direction please?

async def on_member_update(before,after):
    if len(before.roles) < len(after.roles):
        newRole = next(role for role in after.roles if role not in before.roles)
        if newRole.name == "Trial Request":
            channel = discord.utils.get(before.guild.channels, name="trial-request")
            currentMember = discord.utils.get(after.guild.members, id = after.id)
            await channel.send (f"Hi there {currentMember.mention}, I see you have requested the ""Trial Request"" role. Please submit a trial request by clicking at the top of this channel and an officer will be with you in due course. This notification will be automatically deleted after 48 hours. Alternatively, if this was a mistake, please return to the <id:customize> channel. ",delete_after=172800),
    if len(after.roles) < len(before.roles):
        lostRole = next (role for role in before.roles if role not in after.roles)
        if lostRole.name == "Trial Request":
            channel = discord.utils.get(before.guild.channels, name="trial-request")
            currentMember = discord.utils.get(after.guild.members, id = after.id)
            async for message in channel.history(limit=None):
                if (f"Hi there {currentMember.mention}, I see you have requested the ""Trial Request""") in message.content:
                    await message.delete()```
thorny trellis
#

did you try with a print as your first line to make sure if your eceive the event at all

verbal ore
#

It's not picking up with a breakpoint in the code

#

(so the short answer is no, but I was under the impression that a breakpoint would work the same)

verbal ore
#

Okay. I think i know what the issue is. The channels & roles interaction in question adds multiple roles, of which trial request is one. Could my code not be handling that properly? I did think I was looking through the new roles but I could be wrong.

frigid fractal
#

Does discord even call the event for onboarding roles?

#

But your checking and getting can also be a bit simpler:

if after.roles != before.roles:
    lost_roles = list(set(before.roles) - set(after.roles))
    new_roles = list(set(after.roles) - set(before.roles))
    print(new_roles)
thorny trellis
verbal ore
#

So if I remove all the roles apart from trial request on the interaction button

#

it works

#

which suggests to me that my checking is wrong, but I can't see why

#

i'll check it again in the morning when i've slept, but I appreciate the input so far

thorny trellis
#

didnt you say it doesnt enter the function at all

verbal ore
#

I did, and that was true when I raised the thread. Nothing in my last two commits should have changed that, as I was altering other functions, but then again i'm giga tired and should probably be doing this on a fresh head

thorny trellis
#

yea thats usually good

verbal ore
#

apologies, not trying to waste your guys time. Ill re-verify in the morning and then likely approach it from a testing the role iteration side of it

thorny trellis
#

its fine, dw

verbal ore
#

so THIS is triggering for the first role

#

but is not iterating

#

or at least, the check for newRole.name is not part of the iteration

#

Fixed it.

#

those 3 lines I changed to

           newRole = role
           if newRole.name == "Trial Request":```
#

guessing I could take the newRole out as well and isolate only the new roles too but i'll do that in the morning.

thorny trellis
#

thatll trigger for ANY role change as long as the user has that role tho

verbal ore
#

yeah valid point, there's no checking that trial requested is part of the new roles

verbal ore
#

Fixed it, because I can't leave something unfinished. Final block:

async def on_member_update(before, after):
    if len(before.roles) < len(after.roles):
        rolelist = (role for role in after.roles if role not in before.roles)
        for role in rolelist:
            if role.name == "Trial Request":
                channel = discord.utils.get(before.guild.channels, name="trial-request")
                currentMember = discord.utils.get(after.guild.members, id=after.id)
                await channel.send(f"Hi there {currentMember.mention}, I see you have requested the ""Trial Request"" role. Please submit a trial request by clicking at the top of this channel and an officer will be with you in due course. This notification will be automatically deleted after 48 hours. Alternatively, if this was a mistake, please return to the <id:customize> channel. ",delete_after=172800,),
    if len(after.roles) < len(before.roles):
        rolelist = (role for role in before.roles if role not in after.roles)
        for role in rolelist:
            if role.name == "Trial Request":
                channel = discord.utils.get(before.guild.channels, name="trial-request")
                currentMember = discord.utils.get(after.guild.members, id=after.id)
                async for message in channel.history(limit=None):
                    if (f"Hi there {currentMember.mention}, I see you have requested the ""Trial Request""") in message.content:await message.delete()```
#

Effectively I changed the logic from "next" to allocating the array to a variable and then iterating through the elements in the variable.