I am creating a daily streak command for my Discord bot using Python and the discord.py library. The code I'm using is based on an example I found online but I'm facing an issue.
(Code taken from - Economy Bot Daily Streak)
import datetime from datetime import datetime, timedelta @client.command() async def daily(ctx): with open ("streak.json","r") as f: data = json.load(f) streak=data[f"{ctx.author.id}"]["streak"] last_claim_stamp=data[f"{ctx.author.id}"]["last_claim"] last_claim=datetime.fromtimestamp(float(last_claim_stamp)) now =datetime.now() delta = now-last_claim print(f"{streak}\n{last_claim_stamp}\n{last_claim}\n{now}\n{delta}") if delta< timedelta(hours=24): await ctx.send('YOU ALREADY CLAIMED YOUR DAILY in 24 hours') return if delta > timedelta(hours=48): print('streak reset') streak = 1 else: streak+=1 daily = 45+(streak*5) data[f'{ctx.author.id}']["streak"]=streak data[f'{ctx.author.id}']["last_claim"]= str(now.timestamp()) with open("streak.json","w") as f: json.dump(data,f,indent=2) embed = discord.Embed(title="Daily", colour=random.randint(0, 0xffffff), description=f"You've claimed your daily of **{daily}** \n Not credited") embed.set_footer(text=f"Your daily streak : {streak}") await ctx.send(embed=embed) My question is: how can I automatically add users to the JSON file when they first use the daily streak command? I tried creating a function to handle this, but I'm encountering an error where it adds timestamps before the command is executed, resulting in the bot saying "you already claimed your daily" even on the first attempt.
- Please explain how to properly add users to the JSON file so the bot can track their individual streaks.
- If possible, could you provide a code example demonstrating how to achieve automatic user addition?
Источник: https://stackoverflow.com/questions/704 ... nd-in-a-di