I have a Discord.py economy bot that includes a daily command
It gives everyone each day $50, but there is also a streak system. The first time they claim their daily, the bot gives them $50, day 2 is $55, day 3 is $60. And more. If they didn't claim their daily in 24 hours, their streak will be removed and their daily will be back to $50
But I don't really know how to make a daily streak system, can anyone help? (I'm using JSON to store data)
This is my daily command's code:
@bot.command()
@commands.check(user)
@commands.cooldown(1, 86400, commands.BucketType.user)
async def daily(ctx):with open("json/data.json", "r") as f:data = json.load(f)streak = data[f"{ctx.author.id}"]["streak"]streak += 1daily = 45 + (streak * 5)amount_after = data[f"{ctx.author.id}"]["balance"] + dailydata[f"{ctx.author.id}"]["streak"] += 1data[f"{ctx.author.id}"]["balance"] += dailywith open("json/data.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}**, now you have **${amount_after}**")embed.set_footer(text=f"Your daily streak: {streak}")await ctx.send(embed=embed)
Thank you!
so you can use datetime
to check when the last claim was
import datetime
from datetime import datetime, timedeltanow = datetime.now() # a datetime.datetime objekt
last_claim_stamp = str(now.timestamp()) # save this into json
last_claim = datetime.fromtimestamp(float(last_claim_stamp) # get a datetime.datetime backdelta = now - last_claim # the timedelta between now and the last claim
if delta > timedelta(hours=48): # if last claim is older than 48h; 24h until he can re use the command + 24h time to claim his daily money again = 48hstreak = 1 # reset the streak
else:streak += 1
update your data to sth like this:
data = {"1234567890": {"streak": 4,"balance": 50,"last_claim": "1623593996.659298"}
}
command:
@bot.command()
@commands.check(user)
@commands.cooldown(1, 86400, commands.BucketType.user)
async def daily(ctx):with open("json/data.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_claimif delta > timedelta(hours=48):print("reset streak")streak = 1else:print("increase streak")streak += 1daily = 45 + (streak * 5)amount_after = data[f"{ctx.author.id}"]["balance"] + dailydata[f"{ctx.author.id}"]["streak"] = streakdata[f"{ctx.author.id}"]["balance"] += dailydata[f"{ctx.author.id}"]["last_claim"] = str(now.timestamp())with open("json/data.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}**, now you have **${amount_after}**")embed.set_footer(text=f"Your daily streak: {streak}")await ctx.send(embed=embed)