0
votes

I have a background loop that will spit out an emoji every X amount of minutes with a reaction attached to it. I want for when someone presses on the reaction of the message, it will delete the message and then send another message saying "messageauthor has grabbed the loot" and then add the amount to the cash json file.

Right now, my code is making the background loop work, but I am not sure how to grab the message.author.id in regards to the background loop so I can reference it in on_reaction_add. The current code is making the bot react once when it spits out the background loop and then again in on_reaction_add. I'm trying to make it wait for a user to react to the background loop message with the same emoji and not the bot.

client = discord.Client()
emoji_msg_grab = {}


try:
    with open("cash.json") as fp:
        cash = json.load(fp)
except Exception:
    cash = {}

def save_cash():
    with open("cash.json", "w+") as fp:
        json.dump(cash, fp, sort_keys=True, indent=4)

def add_dollars(user: discord.User, dollars: int):
    id = user.id
    if id not in cash:
        cash[id] = {}
    cash[id]["dollars"] = cash[id].get("dollars", 0) + dollars
    print("{} now has {} dollars".format(user.name, cash[id]["dollars"]))
    save_cash()


async def background_loop():
    await client.wait_until_ready()
    while not client.is_closed:
        channel = client.get_channel("479919577279758340")
        emojigrab = '????'
        emojimsgid = await client.send_message(channel, emojigrab)
        await client.add_reaction(emojimsgid, "????")
        user_id = emojimsgid.author.id
        emoji_msg_grab[user_id] = {"emoji_msg_id": emojimsgid.id, "emoji_user_id": user_id}
        await asyncio.sleep(600)

@client.event
async def on_reaction_add(reaction, user):
    msgid = reaction.message.id
    chat = reaction.message.channel

    if reaction.emoji == "????" and msgid == emoji_msg_grab[user.id]["emoji_msg_id"] and user.id == emoji_msg_grab[user.id]["emoji_user_id"]:
        emoji_msg_grab[user.id]["emoji_msg_id"] = None
        await client.send_message(chat, "{} has grabbed the loot!".format(user.mention))
        await client.delete_message(reaction.message)
        add_dollars(user, 250)

client.loop.create_task(background_loop())
1

1 Answers

1
votes

I would use Client.wait_for_reaction instead of on_reaction_add:

async def background_loop():
    await client.wait_until_ready()
    channel = client.get_channel("479919577279758340")
    while not client.is_closed:
        emojigrab = '💰'
        emojimsg = await client.send_message(channel, emojigrab)
        await client.add_reaction(emojimsg, "💵")
        res = await client.wait_for_reaction(emoji="💵", message=emojimsg, timeout=600, 
                                             check=lambda reaction, user: user != client.user)
        if res:  # not None
            await client.delete_message(emojimsg)
            await client.send_message(channel, "{} has grabbed the loot!".format(res.user.mention))
            await asyncio.sleep(1)
            add_dollars(res.user, 250)