经济机器人每日连胜

时间:2021-06-14 05:28:28

标签: python discord discord.py

我有一个包含日常命令的 Discord.py 经济机器人

它每天给每个人 50 美元,但也有一个连胜系统。他们第一次每天领取时,机器人给他们 50 美元,第 2 天是 55 美元,第 3 天是 60 美元。和更多。如果他们没有在 24 小时内领取每日奖金,他们的连胜纪录将被取消,并且每日将恢复到 50 美元

但是我真的不知道如何制作每日连胜系统,有人可以帮忙吗? (我使用 JSON 来存储数据)

这是我的日常命令代码:

@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 += 1
  daily = 45 + (streak * 5)
  amount_after = data[f"{ctx.author.id}"]["balance"] + daily
  data[f"{ctx.author.id}"]["streak"] += 1
  data[f"{ctx.author.id}"]["balance"] += daily
  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)

谢谢!

2 个答案:

答案 0 :(得分:0)

使用 json 作为数据库不是首选,但由于您想继续,您可以节省某人执行 daily 命令的时间,然后您可以比较该人在冷却后使用该命令的时间。

如果差异小于 24 小时或任何您想要的,那么您可以根据连续记录奖励他们更多的钱,例如 amount + (streak * 10),如果差异超过 24 小时,那么您可以清除保存的日期.

答案 1 :(得分:0)

因此您可以使用 datetime 来检查上次声明的时间

import datetime
from datetime import datetime, timedelta

now = 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 back

delta = 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 = 48h
   ​streak = 1 # reset the streak
else:
   ​streak += 1

像这样更新你的数据:

data = {
    "1234567890": {
        "streak": 4,
        "balance": 50,
        "last_claim": "1623593996.659298"
    }
}

命令:

@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_claim
   ​if delta > timedelta(hours=48):
       ​print("reset streak")
       ​streak = 1
   ​else:
       ​print("increase streak")
       ​streak += 1
   ​daily = 45 + (streak * 5)
   ​amount_after = data[f"{ctx.author.id}"]["balance"] + daily
   ​data[f"{ctx.author.id}"]["streak"] = streak
   ​data[f"{ctx.author.id}"]["balance"] += daily
   ​data[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)