diff --git a/src/clients/discord.ts b/src/clients/discord.ts index e65f1fd..b86f640 100644 --- a/src/clients/discord.ts +++ b/src/clients/discord.ts @@ -1,3 +1,5 @@ +import { RESTPostAPIChannelMessageJSONBody } from "discord-api-types/v10"; + export class DiscordClient { private BASE_URL = "https://discord.com/api/v10/"; private config: { headers: Record }; @@ -13,11 +15,11 @@ export class DiscordClient { async sendMessage({ channelId, - content, - }: { channelId: string; content: string }) { + body, + }: { channelId: string; body: RESTPostAPIChannelMessageJSONBody }) { await fetch(`${this.BASE_URL}/channels/${channelId}/messages`, { method: "POST", - body: JSON.stringify({ content }), + body: JSON.stringify(body), headers: this.config.headers, }); } diff --git a/src/scheduled.ts b/src/scheduled.ts index 53df427..04fbceb 100644 --- a/src/scheduled.ts +++ b/src/scheduled.ts @@ -10,30 +10,33 @@ const scheduled: ExportedHandler["scheduled"] = async ( event, env, ) => { - const eventsRepository = new EventsRepository(env.DB); - - // もくもく会が開催されている日のみ実行 - const todayEvent = await eventsRepository.findTodayEvent(); - if (!todayEvent) { - return; - } - const client = new DiscordClient(env.DISCORD_TOKEN); + const eventsRepository = new EventsRepository(env.DB); + const isEventDay = !!(await eventsRepository.findTodayEvent()); + switch (event.cron) { case "0 6 * * *": - client.sendMessage({ - channelId: env.MOKUMOKU_CHANNEL_ID, - content: - "@here テックトークの時間です!発表希望者はこのメッセージに🙋‍♂️でリアクションしてください。", - }); + if (isEventDay) { + await client.sendMessage({ + channelId: env.MOKUMOKU_CHANNEL_ID, + body: { + content: + "@here テックトークの時間です!発表希望者はこのメッセージに🙋‍♂️でリアクションしてください。", + }, + }); + } break; case "50 8 * * * ": - client.sendMessage({ - channelId: env.MOKUMOKU_CHANNEL_ID, - content: - "@here もくもく会終了の時間です!今日の成果を共有しましょう!🍻", - }); + if (isEventDay) { + await client.sendMessage({ + channelId: env.MOKUMOKU_CHANNEL_ID, + body: { + content: + "@here もくもく会終了の時間です!今日の成果を共有しましょう!🍻", + }, + }); + } break; } };