[英]Make a chronomter with discord.py
我想用 Discord.py 制作一個計時器。 我開始編寫腳本,但結果出現問題
我想確定兩個命令之間的時間(以小時為單位)。 但是對於日期時間,strftime 不起作用。
@bot.command()
async def jeu(ctx):
channel = ctx.channel
now = datetime.now()
await ctx.send(f'Bon jeu !')
def check(m):
return m.content == '.deco' and m.channel == channel
msg = await bot.wait_for('message', check=check)
if msg:
then = datetime.now()
delta = now - then
await ctx.send(f'Temps écoulé : {delta}')
(PS:我是法國人,因為我的英語不好x))
您必須將值保留在全局變量中 - 這意味着在函數之外 - 因為此時您將其保存在局部變量中並且您無法在函數外部訪問它。
last_executed = None # default value at start
@bot.command()
async def jeu(ctx):
global last_executed # inform function that it has to assign value to external variable
channel = ctx.channel
last_executed = datetime.now()
await ctx.send(f'Bon jeu !')
# ... code ..
if msg:
if last_executed is None:
await ctx.send('Never executed')
else:
current_time = datetime.now()
delta = last_executed - current_time
await ctx.send(f'Delta time: {delta}')
如果你為不同的用戶運行它,那么你可能需要字典 - 像這樣的東西(但我沒有測試它所以它可能需要一些更改)
last_executed = dict() # default value at start
@bot.command()
async def jeu(ctx):
global last_executed # inform function that it has to assign value to external variable
channel = ctx.channel
last_executed[user_id] = datetime.now()
await ctx.send(f'Bon jeu !')
# ... code ..
if msg:
if user_id not in last_executed:
await ctx.send('Never executed')
else:
current_time = datetime.now()
delta = last_executed[user_id] - current_time
await ctx.send(f'Delta time: {delta}')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.