diff --git a/src/discord.ts b/src/discord.ts index cd7574f..f6cb862 100644 --- a/src/discord.ts +++ b/src/discord.ts @@ -71,12 +71,19 @@ export async function getForumPosts( const posts = await Promise.all( allThreads.map(async (thread: any) => { const msgRes = await fetch( - `https://discord.com/api/v10/channels/${thread.id}/messages/${thread.id}`, + `https://discord.com/api/v10/channels/${thread.id}/messages?limit=10`, { headers }, ); - if (!msgRes.ok) return null; - const msg = await msgRes.json() as any; - return { ...msg, threadId: thread.id }; + if (!msgRes.ok) { + const err = await msgRes.json(); + console.log(`[discord] failed to fetch messages threadId=${thread.id} status=${msgRes.status} err=${JSON.stringify(err)}`); + return null; + } + const messages = await msgRes.json() as any[]; + console.log(`[discord] threadId=${thread.id} messageCount=${messages.length} contents=${JSON.stringify(messages.map((m: any) => ({ id: m.id, bot: m.author?.bot, content: m.content })))}`); + const msg = messages.reverse().find((m: any) => !m.author?.bot); + if (!msg) return null; + return { ...msg, threadId: thread.id, threadName: thread.name }; }), ); diff --git a/src/index.ts b/src/index.ts index 026795e..69df019 100644 --- a/src/index.ts +++ b/src/index.ts @@ -137,23 +137,19 @@ interface ParsedJoinMessage { } function parseJoinMessage(content: string): ParsedJoinMessage | null { - const fields: Record = {}; - - for (const line of content.split("\n")) { - const colonIndex = line.indexOf(":"); - if (colonIndex === -1) continue; - const key = line.slice(0, colonIndex).trim().toLowerCase(); - const value = line.slice(colonIndex + 1).trim(); - if (key && value) fields[key] = value; - } + const normalized = content.replace(/:/g, ":"); - const githubUsername = fields["github"]; - const team = fields["team"]; - const role = fields["role"]; + const githubMatch = normalized.match(/\bgithub\s*:\s*([^\s:]+)/i); + const teamMatch = normalized.match(/\bteam\s*:\s*([^\s:]+)/i); + const roleMatch = normalized.match(/\brole\s*:\s*([^\s:]+)/i); - if (!githubUsername || !team || !role) return null; + if (!githubMatch || !teamMatch || !roleMatch) return null; - return { githubUsername, team, role }; + return { + githubUsername: githubMatch[1], + team: teamMatch[1], + role: roleMatch[1], + }; } async function handleChannelJoin(env: Env): Promise { @@ -169,15 +165,16 @@ async function handleChannelJoin(env: Env): Promise { for (const msg of unprocessed) { const threadId = msg.threadId as string; const content = msg.content ?? ""; - console.log(`[cron] threadId=${threadId} content=${JSON.stringify(content)}`); - const parsed = parseJoinMessage(content); + const threadName = msg.threadName ?? ""; + console.log(`[cron] threadId=${threadId} content=${JSON.stringify(content)} threadName=${JSON.stringify(threadName)}`); + const parsed = parseJoinMessage(content) ?? parseJoinMessage(threadName); if (!parsed) { console.log(`[cron] parse failed threadId=${threadId}`); await replyToMessage( threadId, msg.id, - "⚠️ 메시지 형식이 올바르지 않습니다. 아래 형식으로 다시 작성해주세요.\n```\ngithub: your_github_username\nteam: team_name\nrole: role_name\n```", + "⚠️ 메시지 형식이 올바르지 않습니다. 포스트 **제목(title)** 에 아래 형식으로 한 줄로 작성해주세요.\n```\ngithub: username team: teamname role: rolename\n```", env.DISCORD_BOT_TOKEN, ); await addReaction(threadId, msg.id, "❌", env.DISCORD_BOT_TOKEN);