'discord.py, json | temprole command

So I have this temp role command and I have made it to save the member id and time for the role in a JSON file so the command can't be run on a single member twice, until their time is over. But whenever I restart the bot and if a member is still yet to be unrolled, then it doesn't work. Btw I'm on replit. Code is below:


time_convert = {"s":1, "m":60, "h":3600,"d":86400}
@client.command()
@commands.guild_only()
async def temp(ctx, member:discord.Member=None, time2=None):
 guild = ctx.guild
 role = discord.utils.get(guild.roles, name="Role")
 
 if member == None:
   return await ctx.send("You have to mention the member that you want to add the role to!")
 if time2 == None:
    return await ctx.send("You have to type the time for which you want to give the role to the mentioned member!")
 with open("data.json") as f:
   data = json.load(f)
 if str(member.id) in data:
    return await ctx.send("hmm")
 with open("data.json") as f:
   data = json.load(f)
   data[member.id] = {"time": time2, "role": role.mention}
 with open("data.json", "w") as f:
    json.dump(data, f)
 with open("data.json", "r") as f:
   data = json.load(f)
   time3 = data[str(member.id)]["time"]

 temprole = int(time3[:-1]) * time_convert[time3[-1]]
 await member.add_roles(role)
 await ctx.send(f"Successfully added the `{role.name}` role to **{member.name}#{member.discriminator}** for {time3}")
 await asyncio.sleep(temprole)
 await member.remove_roles(role)
 with open("data.json") as f:
   data = json.load(f)
   data.pop(str(member.id))
 with open("data.json", "w") as f:
    json.dump(data, f) 
 await ctx.send("done")


Solution 1:[1]

import asyncio
import json

time_convert = {"s":1, "m":60, "h":3600,"d":86400}

@client.command()
async def temprole(ctx, member:discord.Member=None, time2=None):
    guild = ctx.guild
    role = discord.utils.get(guild.roles, id = ROLE_ID)
     
    if member == None:
        return await ctx.send("You have to mention the member that you want to add the role to!")
    elif time2 == None:
        return await ctx.send("You have to type the time for which you want to give the role to the mentioned member!")

    with open("temprole.json") as f:
        data = json.load(f)
    if member.id in data:
        return await ctx.send("hmm")
    else:
        data[member.id] = {"time": time2, "role": member.id}
        with open("temprole.json", "w") as f:
            json.dump(data, f)
    with open("temprole.json") as f:
        data = json.load(f)
        time3 = data[str(member.id)]["time"]

    temprole = int(time3[:-1]) * time_convert[time3[-1]]
    await member.add_roles(role)
    await ctx.send(f"Successfully added the `{role.name}` role to **{member.name}#{member.discriminator}** for {time3}")
    await asyncio.sleep(temprole)
    await member.remove_roles(role)
    with open("temprole.json") as f:
        data = json.load(f)
    data.pop(str(member.id))
    with open("temprole.json", "w") as f:
        json.dump(data, f)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rmax Graphics