1
votes

Is it possible for on_reaction_add() to work with messages sent in on_ready()?

I'm trying to get users to verify they are human from a reaction that is on a message sent when the bot starts.

async def on_ready():

#delete msgs from verify chan

    channel = client.get_channel("32133132") 
    msg=[]
    async for x in client.logs_from(channel, limit = 100):
        msg.append(x)
    await client.delete_messages(msg)

#send verification msg w/ reaction

    await client.send_message(channel, "**Verify you are human**")
    verifymsg2 = await client.send_message(channel, "React with ✅ to gain access to Hard Chats.")
    await client.add_reaction(verifymsg2, "✅")


@client.event
async def on_reaction_add(reaction, user):
    channel = client.get_channel('32133132')
    if reaction.message.channel.id != channel:
        return
    else:
        if reaction.emoji == "✅":
            unverified = discord.utils.get(user.server.roles, id="567859230661541927")
            verified = discord.utils.get(user.server.roles, id="567876192229785610")
            await client.remove_roles(user, unverified)
            await client.add_roles(user, verified)
1
It might make more sense to use wait_for_reaction in on_ready instead.Patrick Haugh

1 Answers

0
votes

After messing with this code for a while, I went ahead and did it this way. Every time a new person joins the server, it adds a role of unverified with restricted permissions for each channel. Then it will clear the verify channel and sends the verification message. From there it will wait for a reaction. If reaction, then it will delete unverified role and add verified role.

@client.event
async def on_member_join(member):

    #-------------------------ADD UNVERIFIED ROLE----------------------------#
    role = discord.utils.get(member.server.roles, id="123456789")
    await client.add_roles(member, role)

    #---------------DELETE MSG FROM VERIFY---------------# 

    channel = client.get_channel("0987654321") 
    msg=[]
    async for x in client.logs_from(channel, limit = 100):
        msg.append(x)
    await client.delete_messages(msg)

    #---------------SEND VERIFY MSG / REACT TO VERIFY MSG---------------# 

    await client.send_message(channel, "**Verify you are human**")
    verifymsg2 = await client.send_message(channel, "React with ✅ to gain access to Hard Chats.")
    await client.add_reaction(verifymsg2, "✅")
    unverified = discord.utils.get(member.server.roles, name="unverified")
    verified = discord.utils.get(member.server.roles, name="verified")
    reaction = await client.wait_for_reaction(emoji="✅", message=verifymsg2, 
                                            check=lambda reaction, user: user != client.user)
    if reaction:
        await client.remove_roles(member, unverified)
        await asyncio.sleep(0.5)
        await client.add_roles(member, verified)