feat: auto merge

master
paring 2023-06-29 01:39:54 +09:00
parent a7dd7869f2
commit 8b75aef1fe
Signed by: pikokr
GPG Key ID: 8C7ABCEF704FD728
2 changed files with 76 additions and 3 deletions

View File

@ -1,4 +1,10 @@
import { client, gateway, sessionManager, voiceStateManager } from "./api.js" import {
client,
db,
gateway,
sessionManager,
voiceStateManager,
} from "./api.js"
import { import {
ActivityType, ActivityType,
@ -14,6 +20,9 @@ import { config } from "./config.js"
import { syncCommands } from "./utils/commands.js" import { syncCommands } from "./utils/commands.js"
import { VoiceState } from "./utils/wrapper/VoiceState.js" import { VoiceState } from "./utils/wrapper/VoiceState.js"
import { isMe } from "./utils/appid.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("uncaughtException", (error) => console.error(error))
process.on("unhandledRejection", (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() session.vc.destroy()
sessionManager.removeSession(data.guild_id) sessionManager.removeSession(data.guild_id)
await api.channels.createMessage(session.textChannelId, { 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)
} }
} }
} }

View File

@ -3,7 +3,10 @@ import { db } from "../api.js"
import { spawn } from "child_process" import { spawn } from "child_process"
import { existsSync, mkdirSync } from "fs" 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 finalDir = "recordings/final"
const sourceDir = path.resolve("recordings", id) const sourceDir = path.resolve("recordings", id)
@ -60,6 +63,8 @@ export const mergeAudio = async (id: string) => {
const p = spawn("ffmpeg", args).on("exit", (code) => { const p = spawn("ffmpeg", args).on("exit", (code) => {
console.log("ffmpeg exited with code", code) console.log("ffmpeg exited with code", code)
callback?.(code)
}) })
p.stderr.pipe(process.stderr) p.stderr.pipe(process.stderr)
} }