Discord: refine voice message handling

This commit is contained in:
Shadow
2026-02-13 12:33:45 -06:00
committed by Shadow
parent 76ab377a19
commit 1c9c01ff49
5 changed files with 93 additions and 39 deletions

View File

@@ -50,7 +50,9 @@ export async function getAudioDuration(filePath: string): Promise<number> {
}
return Math.round(duration * 100) / 100; // Round to 2 decimal places
} catch (err) {
throw new Error(`Failed to get audio duration: ${err instanceof Error ? err.message : err}`);
throw new Error(`Failed to get audio duration: ${err instanceof Error ? err.message : err}`, {
cause: err,
});
}
}
@@ -104,7 +106,7 @@ async function generateWaveformFromPcm(filePath: string): Promise<string> {
let sum = 0;
let count = 0;
for (let j = 0; j < step && i * step + j < samples.length; j++) {
sum += Math.abs(samples[i * step + j]!);
sum += Math.abs(samples[i * step + j]);
count++;
}
const avg = count > 0 ? sum / count : 0;
@@ -225,39 +227,27 @@ export async function sendDiscordVoiceMessage(
metadata: VoiceMessageMetadata,
replyTo: string | undefined,
request: RetryRunner,
token: string,
silent?: boolean,
): Promise<{ id: string; channel_id: string }> {
const filename = "voice-message.ogg";
const fileSize = audioBuffer.byteLength;
// Step 1: Request upload URL (using fetch directly for proper Content-Type header)
// Wrapped in retry runner for consistency with other Discord API calls
const uploadUrlResponse = await request(async () => {
const res = await fetch(`https://discord.com/api/v10/channels/${channelId}/attachments`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${token}`,
},
body: JSON.stringify({
files: [
{
filename,
file_size: fileSize,
id: "0",
},
],
}),
});
if (!res.ok) {
const errorBody = await res.text();
throw new Error(`Failed to get upload URL: ${res.status} ${errorBody}`);
}
return (await res.json()) as UploadUrlResponse;
}, "voice-upload-url");
// Step 1: Request upload URL from Discord
const uploadUrlResponse = await request(
() =>
rest.post(`/channels/${channelId}/attachments`, {
body: {
files: [
{
filename,
file_size: fileSize,
id: "0",
},
],
},
}) as Promise<UploadUrlResponse>,
"voice-upload-url",
);
if (!uploadUrlResponse.attachments?.[0]) {
throw new Error("Failed to get upload URL for voice message");