【问题标题】:Economy Bot Daily Streak经济机器人每日连胜
【发布时间】:2021-08-30 01:30:20
【问题描述】:

我有一个包含日常命令的 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)

谢谢!

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    所以您可以使用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)
    

    【讨论】:

    • 没问题 :D 你可以接受答案 ;)
    【解决方案2】:

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

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

    【讨论】:

    • 您好,您如何节省执行命令的人的时间,例如,是否有任何模块?
    • 您可以使用 ctx.message.created_at 或 on_command_completion 事件,该事件仅在命令成功且所有检查均已通过时触发。在此处查看其文档:discordpy.readthedocs.io/en/stable/ext/commands/…
    猜你喜欢
    • 2021-08-11
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2021-05-25
    • 2022-01-24
    • 2021-03-13
    • 2016-03-29
    相关资源
    最近更新 更多