From 601ebca93e12aa839758a4cdae1de8fc37913075 Mon Sep 17 00:00:00 2001 From: imnyang Date: Sat, 4 Apr 2026 00:06:19 +0900 Subject: [PATCH] Update timetable function to add input validation and improve error handling --- app/index.ts | 3 ++- app/lib/discord.ts | 32 +++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/app/index.ts b/app/index.ts index f6bf9db..06d4fc1 100644 --- a/app/index.ts +++ b/app/index.ts @@ -9,7 +9,8 @@ async function main() { const YYYY = now.getFullYear(); const MM = String(now.getMonth() + 1).padStart(2, '0'); const DD = String(now.getDate()).padStart(2, '0'); - const YYMMDD = `${YYYY}${MM}${DD}`; + // const YYMMDD = `${YYYY}${MM}${DD}`; + const YYMMDD = `20260403`; // getDay(): 일(0) ~ 토(6) // comcigan.ts 라이브러리 기준에 맞춰 weekday 설정 필요 diff --git a/app/lib/discord.ts b/app/lib/discord.ts index 28f153f..0e61d74 100644 --- a/app/lib/discord.ts +++ b/app/lib/discord.ts @@ -40,22 +40,44 @@ export async function Meal({ MLSV_YMD, ATPT_OFCDC_SC_CODE, SD_SCHUL_CODE, userna export async function Timetable({ schoolId, grade, classNum, weekday, WEBHOOK_URL }: { schoolId: number, grade: number, classNum: number, weekday: number, WEBHOOK_URL: string }) { - const timetableInfo = await getTimetable({ schoolId, grade, classNum, weekday }); + // 필수 값 검증: undefined/null 이면 전송 안 함 + if ([schoolId, grade, classNum, weekday, WEBHOOK_URL].some((v) => v === undefined || v === null)) { + console.warn("⚠️ | Undefined input detected. Skip sending webhook."); + return; + } + const weekdayText = ["월", "화", "수", "목", "금"][weekday - 1]; + if (!weekdayText) { + console.warn("⚠️ | Invalid weekday. Skip sending webhook."); + return; + } + + const timetableInfo = await getTimetable({ schoolId, grade, classNum, weekday }); console.log("🏓 | Timetable Info Retrieved", timetableInfo); + + // 받아온 시간표 값이 없거나(undefined) 항목에 undefined 값이 있으면 전송 안 함 + if ( + !timetableInfo || + timetableInfo.length === 0 || + timetableInfo.some((item) => !item || item.subject === undefined || item.teacher === undefined) + ) { + console.warn("⚠️ | Timetable contains undefined/empty data. Skip sending webhook."); + return; + } + const data = { content: `📅 | ${grade}학년 ${classNum}반 시간표 정보`, embeds: [ { title: `🏫 | 학교 : 선린인터넷고등학교`, - fields: (timetableInfo ?? []).map((item) => ({ - name: `${item.subject}${item.changed ? (" *") : ""}`, + fields: timetableInfo.map((item) => ({ + name: `${item.subject}${item.changed ? " *" : ""}`, value: `${item.teacher}${item.subject in room ? ` | ${room[item.subject as keyof typeof room]}` : ""}`, inline: false, })), footer: { - text: `${["월", "화", "수", "목", "금"][weekday - 1]}요일 시간표 정보` - } + text: `${weekdayText}요일 시간표 정보`, + }, }, ], };