diff --git a/src/index.ts b/src/index.ts index 5ca47f3..40939dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,10 @@ -import { client, gateway, sessionManager, voiceStateManager } from "./api.js" +import { + client, + db, + gateway, + sessionManager, + voiceStateManager, +} from "./api.js" import { ActivityType, @@ -14,6 +20,9 @@ import { config } from "./config.js" import { syncCommands } from "./utils/commands.js" import { VoiceState } from "./utils/wrapper/VoiceState.js" import { isMe } from "./utils/appid.js" +import { mergeAudio } from "./utils/merge.js" +import { readFile, rm } from "fs/promises" +import path from "path" process.on("uncaughtException", (error) => console.error(error)) process.on("unhandledRejection", (error) => console.error(error)) @@ -115,8 +124,67 @@ client.on(GatewayDispatchEvents.VoiceStateUpdate, async ({ data, api }) => { session.vc.destroy() sessionManager.removeSession(data.guild_id) await api.channels.createMessage(session.textChannelId, { - content: `${session.id}`, + content: `녹화가 종료되었어요! 오디오 파일 합치는 중...`, }) + setTimeout(async () => { + await mergeAudio(session.id, async (code) => { + if (code === 0) { + let succeeded = false + try { + await api.channels.createMessage( + session.textChannelId, + { + content: + "오디오 파일 합치기가 완료되었어요!", + files: [ + { + contentType: "audio/ogg", + name: `${session.id}.ogg`, + data: await readFile( + path.resolve( + "recordings", + "final", + `${session.id}.ogg` + ) + ), + }, + ], + } + ) + succeeded = true + } catch { + await api.channels.createMessage( + session.textChannelId, + { + content: `오디오 파일 합치기가 완료되었...지만 파일 업로드에 실패했어요.\n파일 위치: ${path.join( + "recordings", + "final", + `${session.id}.ogg` + )}`, + } + ) + } + + if (succeeded) { + await rm( + path.resolve("recordings", session.id), + { recursive: true, force: true } + ) + + await db.session.delete({ + where: { id: session.id }, + }) + } + } else { + await api.channels.createMessage( + session.textChannelId, + { + content: `ffmpeg 명령어 실행에 실패했어요! (exit code ${code}) ID: ${session.id}`, + } + ) + } + }) + }, 1000) } } } diff --git a/src/utils/merge.ts b/src/utils/merge.ts index 5a5063e..be004f6 100644 --- a/src/utils/merge.ts +++ b/src/utils/merge.ts @@ -3,7 +3,10 @@ import { db } from "../api.js" import { spawn } from "child_process" import { existsSync, mkdirSync } from "fs" -export const mergeAudio = async (id: string) => { +export const mergeAudio = async ( + id: string, + callback?: (code: number | null) => void +) => { const finalDir = "recordings/final" const sourceDir = path.resolve("recordings", id) @@ -60,6 +63,8 @@ export const mergeAudio = async (id: string) => { const p = spawn("ffmpeg", args).on("exit", (code) => { console.log("ffmpeg exited with code", code) + + callback?.(code) }) p.stderr.pipe(process.stderr) }