0
votes

Hi this is a cog thats function where the bot sends a message on_member_join and allows the user to react to a post in which the bot adds the user a specific role in this case it's Members. I'm having issues adding a check.

  1. I want to allow the bot to set a pre thumbs up emoji without applying the user role before the user has reacted to the post.

  2. There seems a flaw in this method which is allowing anyone including the bot to react with the thumbs up emoji and giving the joining user the Members role rather than the user themselves. It seems a check is needed to only register the joining user has used the thumbs up reaction.

Is this possible?

Here is what I'm working with:

I have cited the to-dos with # in the code block to give you an idea.

thumbs_up = "\N{THUMBS UP SIGN}"

def react_check(user, msg, emoji):
    def check(reaction, user):
       return user==user and reaction.message.id==msg.id and 
reaction.emoji==emoji
    return check
    class Welcome(commands.Cog):
    def __init__(self, bot):
        self.bot = bot


@commands.Cog.listener()
async def on_member_join(self, user: discord.Member):
    guildName = user.guild.name
    channel = self.bot.get_channel(555844758778544160) 
    embed = discord.Embed(colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
    embed.title = "Hello {}, welcome to {}!".format(user, guildName)
    embed.description = "Before you continue we just want to make sure you have read up on our all important group guidelines. if you haven't you can find them in <#546694486391128066>. Once you're satified with them go ahead and give my message a :thumbsup:."
    embed.set_image(url='')
    msg = await channel.send(embed=embed)
    await msg.add_reaction(thumbs_up) # todo: make bot set a thumbs_up emoji but only set members role when the user mentioned reacts. 
    await self.bot.wait_for('reaction_add', check=react_check(user, msg, thumbs_up))
    role = get(user.guild.roles, name="Members")
    if self.bot: #supposed check to see if the bot 
        pass
    else: 
        await user.add_roles(role)
        await channel.send(':thumbsup: Awesome! you\'re now set to go. If you would like to add some roles head over to <#555913791615926302> for more details. ')# Check 

def setup(bot):
    bot.add_cog(Welcome(bot))
1
You should add the react_check code to this question. Is react_check returning True for users other than user who are reacting to the msg? It should only continue the coroutine when a reaction that meets the criteria of the react_check is given.Patrick Haugh
@PatrickHaugh we spoke on the exact code yesterday in another question. I've gone ahead and added the react_check code in my question. Ideally I'm wanting the bot to add a react to the post the without adding the member role to the user unless the user adds to that reaction. I.e. the bot reacts first but the user reacts second to which a role is applied to that user.James

1 Answers

0
votes

In your react_check you're doing user==user, which will always be true. You need to instead name one of the users something else (In the original code, you'll note I used user and usr, which is probably what confused you):

def react_check(user, msg, emoji):
    def check(reaction, reacting_user):
       return user==reacting_user and reaction.message.id==msg.id and reaction.emoji==emoji
    return check

This code uses a slightly complicated, but very useful, pattern called closures. A closure is basically a technique for creating multiple versions of the same function that use different values for some variables. Here, calling react_check will return a check function that will check reactions against the values that you passed to react_check

Also, self.bot should also always be true, so I don't think that check will accomplish anything.