1
1
import discord
2
- import discord . app_commands as app_commands
2
+ from discord import app_commands
3
3
from discord .ext import commands
4
- from datetime import datetime , timedelta
4
+ from datetime import datetime
5
5
import pytz
6
6
import json
7
7
import re
8
8
9
- weekday_map = {
10
- 'Monday' : '星期一' ,
11
- 'Tuesday' : '星期二' ,
12
- 'Wednesday' : '星期三' ,
13
- 'Thursday' : '星期四' ,
14
- 'Friday' : '星期五' ,
15
- 'Saturday' : '星期六' ,
16
- 'Sunday' : '星期日'
17
- }
18
-
19
-
20
- # 尋找下一節課的邏輯,考慮目前時間是第幾節課
21
-
22
9
class ScheduleCog (commands .Cog ):
23
10
def __init__ (self , bot ):
24
11
self .bot = bot
25
- def load_schedule_data (self , user_name ):
12
+ self .weekday_map = {
13
+ 'Monday' : '星期一' , 'Tuesday' : '星期二' , 'Wednesday' : '星期三' ,
14
+ 'Thursday' : '星期四' , 'Friday' : '星期五' , 'Saturday' : '星期六' , 'Sunday' : '星期日'
15
+ }
16
+
17
+ @app_commands .command (name = "schedule" , description = "查詢課表" )
18
+ @app_commands .describe (
19
+ query_type = "查詢類型(next: 下一節課,now: 當前課程)" ,
20
+ user = "要查詢的用戶(可選,默認為自己)"
21
+ )
22
+ async def schedule_command (self , interaction : discord .Interaction , query_type : str = 'next' , user : discord .User = None ):
23
+ await interaction .response .defer (thinking = True )
24
+ result = await self .query_schedule (user or interaction .user , query_type )
25
+ await interaction .followup .send (result )
26
+
27
+ async def query_schedule (self , user : discord .User , query_type : str = 'next' ,message_to_edit : discord .Message = None ) -> str :
28
+ try :
29
+ if message_to_edit :
30
+ await message_to_edit .edit (content = "看看課表" )
31
+ schedule = self .load_schedule_data (user .id )
32
+ if schedule is None :
33
+ return "找不到課表數據。"
34
+
35
+ tz = pytz .timezone ('Asia/Taipei' )
36
+ now = datetime .now (tz )
37
+ current_hour = now .hour
38
+ current_weekday = now .strftime ('%A' )
39
+ current_class_period = current_hour - 7 # 8點開始為第1節
40
+ current_weekday_cn = self .weekday_map .get (current_weekday , "" )
41
+
42
+ if query_type == 'next' :
43
+ return f'用戶 { user .name } 的課表:{ self .find_next_class (schedule , current_weekday_cn , current_class_period )} '
44
+ elif query_type == 'now' :
45
+ return f'用戶 { user .name } 的課表:{ self .find_current_class (schedule , current_weekday_cn , current_class_period )} '
46
+ else :
47
+ return '無效的查詢類型'
48
+ except Exception as e :
49
+ return f'查詢課表時發生錯誤:{ str (e )} '
50
+
51
+ def load_schedule_data (self , user_id : int ) -> dict :
26
52
try :
27
- match = re .search (r'\d+' , user_name )
28
- user_id = match .group ()
29
53
with open (f'./data/schedule_data/{ user_id } .json' , 'r' , encoding = 'utf-8' ) as f :
30
54
return json .load (f )
31
55
except FileNotFoundError :
32
56
return None
33
-
34
- def find_next_class_corrected (self ,schedule , weekday , current_class_period ):
35
- print ('目前是' ,weekday ,'第' ,current_class_period ,'節' )
57
+
58
+ def find_next_class (self , schedule : dict , weekday : str , current_class_period : int ) -> str :
36
59
if weekday not in schedule :
37
60
return "今日無課程。"
38
- current_class_period = current_class_period if current_class_period > 0 else 0
39
- # 從目前節次開始搜尋下一節有安排的課
61
+ current_class_period = max (current_class_period , 0 )
40
62
for period in range (current_class_period , len (schedule [weekday ])):
41
- if schedule [weekday ][period ]: # 如果這個時間段有課
63
+ if schedule [weekday ][period ]:
42
64
class_info = schedule [weekday ][period ]
43
-
44
65
return f"下一節課是第 { period + 1 } 節 { period + 8 } :10開始,課名:{ class_info ['課名' ]} ,教授:{ class_info ['教授' ]} ,教室:{ class_info ['教室' ]} 。"
45
66
return "今日已無其他課程。"
46
-
47
- def find_current_class (self , schedule , weekday , current_class_period ):
48
- print ('目前是' ,weekday ,'第' ,current_class_period ,'節' )
49
- if schedule is None :
50
- return "找不到課表數據。"
51
-
67
+
68
+ def find_current_class (self , schedule : dict , weekday : str , current_class_period : int ) -> str :
52
69
if weekday not in schedule :
53
70
return "今日無課程。"
54
- # 檢查目前時間對應的課程
55
- if schedule [weekday ][current_class_period - 1 ]:
71
+ if 0 <= current_class_period - 1 < len (schedule [weekday ]) and schedule [weekday ][current_class_period - 1 ]:
56
72
class_info = schedule [weekday ][current_class_period - 1 ]
57
73
return f"目前是第 { current_class_period } 節,課名:{ class_info ['課名' ]} ,教授:{ class_info ['教授' ]} ,教室:{ class_info ['教室' ]} 。"
58
74
else :
59
75
return "現在沒在上課。"
60
-
61
- @commands .hybrid_command (name = "課表" )
62
- @app_commands .describe (user_name = "@用戶名稱,用於查詢特定用戶的課表" )
63
- async def query_schedule (self , ctx , user_name : str = None ):
64
- if user_name is None :
65
- user_name = f"<@{ ctx .author .id } >" # 若未提供ID,則使用命令發送者的ID
66
- schedule = self .load_schedule_data (str (user_name ))
67
- if schedule is None :
68
- await ctx .send ("找不到課表數據。" )
69
- return
70
- # 獲取當前時間和星期
71
- tz = pytz .timezone ('Asia/Taipei' )
72
- now = datetime .now (tz )
73
- current_hour = now .hour
74
- current_weekday = now .strftime ('%A' )
75
- # 計算目前是第幾節課
76
- current_class_period = current_hour - 7 # 8點開始為第1節
77
- # 將英文星期轉換為中文
78
- current_weekday_cn = weekday_map .get (current_weekday , "" )
79
- response = self .find_next_class_corrected (schedule , current_weekday_cn , current_class_period )
80
- await ctx .send (user_name + response )
81
-
82
- @commands .hybrid_command (name = "正在上" )
83
- @app_commands .describe (user_name = "@用戶名稱,用於查詢特定用戶的現在課程" )
84
- async def query_current_class (self , ctx , user_name : str = None ):
85
- if user_name is None :
86
- user_name = f"<@{ ctx .author .id } >" # 若未提供ID,則使用命令發送者的ID
87
- schedule = self .load_schedule_data (str (user_name ))
88
- if schedule is None :
89
- await ctx .send ("找不到課表數據。" )
90
- return
91
- # 獲取當前時間和星期
92
- tz = pytz .timezone ('Asia/Taipei' )
93
- now = datetime .now (tz )
94
- current_hour = now .hour
95
- current_weekday = now .strftime ('%A' )
96
- # 計算目前是第幾節課
97
- current_class_period = current_hour - 7 # 8點開始為第1節
98
- # 將英文星期轉換為中文
99
- current_weekday_cn = weekday_map .get (current_weekday , "" )
100
- response = self .find_current_class (schedule , current_weekday_cn , current_class_period )
101
- await ctx .send (user_name + response )
102
-
103
-
104
76
105
77
async def setup (bot ):
106
- await bot .add_cog (ScheduleCog (bot ))
78
+ await bot .add_cog (ScheduleCog (bot ))
0 commit comments