On_Reaction_Add() with messages sent in On_Ready() Discord.PY

时间:2019-04-17 01:30:51

标签: python python-3.x discord discord.py

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 个答案:

答案 0 :(得分:0)

弄乱这段代码一段时间后,我继续这样做。每次有新用户加入服务器时,它都会添加未经验证的角色,每个通道的权限受限。然后它将清除验证通道并发送验证消息。从那里它将等待反应。如果有反应,它将删除未验证的角色并添加已验证的角色。

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