我怎样才能让这个命令在 3 天内使用一次?
How can i make this command to be used once in 3 days?
我正在执行一个命令,为我的网站发送一个 api 密钥,但我不希望它被滥用,所以我需要在 3 天(72 小时)或 3 天(72 小时)内使用一次命令2 天甚至 1 天我只是不想让它每秒重新生成一次
这是我的 atm 密码
@bot.command()
async def key(ctx):
await ctx.send(next(keys))
顺便说一下,如果您好奇的话,api 密钥在一个 txt 文件中。
你可以看看这个 repo:https://github.com/gawel/aiocron
这允许您使用 discord bot 运行 crontab 作业,如果您想每三天 运行 它,您只需使用这个:
0 12 */3 * *
每 3 天12:00运行宁(您可以更改时间)
您可以将上次访问时间戳保存到文件中并检查冷却时间是否已经过 datetime.timedelta
你可以这样做:
from datetime import datetime, timedelta
@bot.command()
async def key(ctx):
def save_timestamp(timestamp): # Function to save the timestamp to file
timestamp = int(timestamp)
with open("last_credentials.txt", 'w') as f:
f.write(str(timestamp))
try: # Try reading last credentials usage from file 'last_credentials.txt'
with open("last_credentials.txt", 'r') as f:
timestamp = int(f.read())
except FileNotFoundError:
# Create new timestamp if the file does not exist
timestamp = datetime(year=1970, day=1, month=1).timestamp()
creds_datetime = datetime.fromtimestamp(timestamp)
if datetime.now() - creds_datetime > timedelta(hours=72): # check if 3 days have passed from last usage. You could also use days=3 instead of hours=72
save_timestamp(datetime.now().timestamp())
await ctx.send(next(keys))
您也可以将此时间戳存储在内存中,但这样不会保留最后的时间戳,以防您重新启动程序
我正在执行一个命令,为我的网站发送一个 api 密钥,但我不希望它被滥用,所以我需要在 3 天(72 小时)或 3 天(72 小时)内使用一次命令2 天甚至 1 天我只是不想让它每秒重新生成一次
这是我的 atm 密码
@bot.command()
async def key(ctx):
await ctx.send(next(keys))
顺便说一下,如果您好奇的话,api 密钥在一个 txt 文件中。
你可以看看这个 repo:https://github.com/gawel/aiocron
这允许您使用 discord bot 运行 crontab 作业,如果您想每三天 运行 它,您只需使用这个:
0 12 */3 * *
每 3 天12:00运行宁(您可以更改时间)
您可以将上次访问时间戳保存到文件中并检查冷却时间是否已经过 datetime.timedelta 你可以这样做:
from datetime import datetime, timedelta
@bot.command()
async def key(ctx):
def save_timestamp(timestamp): # Function to save the timestamp to file
timestamp = int(timestamp)
with open("last_credentials.txt", 'w') as f:
f.write(str(timestamp))
try: # Try reading last credentials usage from file 'last_credentials.txt'
with open("last_credentials.txt", 'r') as f:
timestamp = int(f.read())
except FileNotFoundError:
# Create new timestamp if the file does not exist
timestamp = datetime(year=1970, day=1, month=1).timestamp()
creds_datetime = datetime.fromtimestamp(timestamp)
if datetime.now() - creds_datetime > timedelta(hours=72): # check if 3 days have passed from last usage. You could also use days=3 instead of hours=72
save_timestamp(datetime.now().timestamp())
await ctx.send(next(keys))
您也可以将此时间戳存储在内存中,但这样不会保留最后的时间戳,以防您重新启动程序