-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
66 lines (47 loc) · 1.52 KB
/
timer.py
File metadata and controls
66 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import discord
from discord import app_commands
import os
from dotenv import load_dotenv
import asyncio
from datetime import datetime, timedelta
load_dotenv()
# TOKEN
TOKEN = os.getenv("BOT_TOKEN")
if TOKEN is None:
print("Error: BOT_TOKEN is not set in .env file.")
exit(1)
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
# 1個だけ持つ
current_task = None
def parse_time(timestr: str):
now = datetime.now()
hour, minute = map(int, timestr.split(":"))
target = now.replace(hour=hour, minute=minute, second=0, microsecond=0)
if target <= now:
target += timedelta(days=1)
return target
async def timer_task(channel_id, user_id, target_time):
delay = (target_time - datetime.now()).total_seconds()
await asyncio.sleep(delay)
channel = client.get_channel(channel_id)
if isinstance(channel, (discord.TextChannel, discord.Thread)):
await channel.send(f"<@{user_id}> 時間です")
@client.event
async def on_ready():
print("起動完了")
await tree.sync()
@tree.command(name="timer")
@app_commands.describe(time="HH:MM")
async def timer(interaction: discord.Interaction, time: str):
global current_task
# target =
# 前のタイマーを消す
if current_task:
current_task.cancel()
# 新しく作る
# current_task = asyncio.create_task(
# )
await interaction.response.send_message() # 時間をセットしたことを伝える
client.run(TOKEN)