经济每日连胜

2024-06-25 23:42:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

它每天给每个人50美元,但也有一个连胜系统。他们第一次申请每日津贴时,机器人会给他们50美元,第二天是55美元,第三天是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)

谢谢大家!


Tags: 命令idjsondata系统with机器人embed
2条回答

因此,您可以使用^{}来检查最后一个声明是何时提交的

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)

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

如果差异小于24小时或您希望的任何时间,则您可以根据条纹(例如amount + (streak * 10))奖励他们更多的钱,如果差异大于24小时,则您可以清除保存的日期

相关问题 更多 >