aishaq11
aishaq11

Reputation: 179

After incrementing data, Discord message sent to user is off by 1

When I increment the win by 1 in the DB, and return the amount of wins the user has, it is always off by 1.

For example in the code below:

import discord
import pymongo

client = discord.Client()
mongo = MongoClient('localhost', 27017)
db = mongo.collection
profiles = db.profiles

@client.event
async def on_message(message):
    if message.author == client.user:
        return

if message.content.startswith('!iwin'):
    profile = profiles.find_one({'discordID': message.author.id})
    updatewin = profiles.find_one_and_update({'discordID': message.author.id}, {"$inc":
                                                                 {"wins": 1, "coins": 40}})
    win_msg = "Your win has been recorded. You have {} wins!".format(profile['wins'])

If I type !iwin, the 1 win gets recorded in the DB, however the message that gets returned is Your win has been recorded. You have 0 wins! And if I type !iwin again, the second win gets recorded, for a total of 2 wins, but the message reads Your win has been recorded. You have 1 wins! It seems to me like the message gets sent prior to the increment happening, how do I get around this?

EDIT It also affects any type of operation I try to add using the wins field. For example:

win_msg = "Your win has been recorded."
await message.channel.send(win_msg)
if profile['wins']%2==0:
    roll = random.randint(1,809)
    roll_msg = "You have won 2 games! Here is your roll: {}. Would you like to !keep or !reroll?".format(roll)
    await message.channel.send(roll_msg)

It always calculates it wrong. So if I have 2 wins in the DB, it doesn't give the roll, but if I have 1 win in the DB, it does calculate the roll.

Upvotes: 0

Views: 34

Answers (1)

Reedinationer
Reedinationer

Reputation: 5774

You can just explicitly add 1 within your string formatting. It would look like:

win_msg = "Your win has been recorded. You have {} wins!".format(int(profile['wins']) + 1)

You probably don't need the int() either, but I usually throw it on so it is clear to both the computer and my future self what my intent was.

From your update it would seem maybe your database actually isn't being updated the way you expect. I'm not sure what your find_one() method returns, but it may be a static value (probably would be a good idea to include in your post). Have you tried verifying it with print statements like this? (Also, I think your if block is supposed to be indented?)

if message.content.startswith('!iwin'):
    profile = profiles.find_one({'discordID': message.author.id})
    print(profile)
    updatewin = profiles.find_one_and_update({'discordID': message.author.id}, {"$inc":{"wins": 1, "coins": 40}})
    print(profile) # has this changed?
    # maybe you need to call your find_one method again? Like
    profile = profiles.find_one({'discordID': message.author.id})
    print(profile) # is it updated now??

Upvotes: 1

Related Questions