所以我正在制作一個不和諧的機器人,我在一個stackoverflow問題上看到了這個代碼
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))
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
@client.command()
@commands.check(user)
@commands.cooldown(1, 86400, commands.BucketType.user)
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
?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("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}**, now you have **${amount_after}**")
?embed.set_footer(text=f"Your daily streak: {streak}")
?await ctx.send(embed=embed)
我在這里遇到錯誤 -
File "main.py", line 1148
?last_claim=datetime.fromtimestamp(float(last_claim_stamp))
^
SyntaxError: invalid character in identifier
我重新輸入了錯誤代碼的起始部分,但仍然沒有成功,請幫助。我使用了另一個解釋器,它顯示了相同的錯誤,但在不同的地方,即 (last_claim_stamp)
uj5u.com熱心網友回復:
您的代碼在名稱的開頭有一個不可見的 Unicode 字符last_claim,特別是 U 200B ZERO WIDTH SPACE
事實上,整段代碼似乎在大多數行的開頭都有 U 200B 字符。
如何解決這個問題更多地取決于你的編輯器/IDE,而不是 Python;您將需要弄清楚如何進行搜索和替換以將它們全部洗掉,并且可能還需要某種查看它們的方法(盡管如果它開始作業,您可能會稱之為完成)。根據他們如何到達那里,您可能還需要避免在將來使用相同的方法,或者更改設定以免生成 U 200B 字符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/390736.html
