0

I'm using Discord.py Rewrite and I want to check if the message that a user reacts to is the same message that my bot sends by using reaction.message == msg in my check function. For some reason, this returns "False" despite having identical results when printing each individually. I can get the check to return True by typecasting each into a string, but I would like to understand why reaction.message is different to msg before typecasting.

def check(reaction, user):
    print("Reaction message: ", reaction.message)
    print("Original Message: ", msg)
    print(msg == reaction.message)
    return user != client.user and msg == reaction.message

msg = await ctx.send("test")
await msg.add_reaction('\u2705')
await msg.add_reaction('\u274C')

reaction, user = await client.wait_for('reaction_add', check = check)
Jeffo
  • 1

1 Answers1

0

Even though string representation of both are same, the objects might be (here they are) different. You might want to check the string content of the message:

    return user != client.user and str(msg) == str(reaction.message)
    # or
    return user != client.user and msg.content == reaction.message.content

A better solution would be to compare message ids instead:

    return user != client.user and msg.id == reaction.message.id

More info in the docs

MePsyDuck
  • 364
  • 2
  • 15
  • What happens if there are messages with the identical message content? For example two messages with the content "Hello!", would reacting to either of the two return 1? – terrabyte Nov 22 '19 at 09:38
  • It should if user is also the same. The best solution would be to compare the message ids, but here author asked for comparing the message. – MePsyDuck Nov 22 '19 at 10:16