refactor: Simplify Discord API response handling and define response interface
This commit is contained in:
@@ -10,6 +10,15 @@ if (!process.env.DISCORD_BOT_TOKEN || !process.env.DISCORD_CHANNEL_ID) {
|
||||
|
||||
const rateLimiter = new DiscordRateLimiter(DISCORD_API_BASE_URL, process.env.DISCORD_BOT_TOKEN);
|
||||
|
||||
interface DiscordMessageResponse {
|
||||
id: string;
|
||||
attachments: Array<{
|
||||
id: string;
|
||||
url: string;
|
||||
size: number;
|
||||
}>;
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const formData = await request.formData();
|
||||
@@ -25,26 +34,13 @@ export async function POST(request: Request) {
|
||||
const discordFormData = new FormData();
|
||||
discordFormData.append('file', chunk, `chunk-${partIndex}.bin`);
|
||||
|
||||
const discordRes: Response = await rateLimiter.request(
|
||||
const discordData = await rateLimiter.request<DiscordMessageResponse>(
|
||||
`/channels/${process.env.DISCORD_CHANNEL_ID}/messages`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: discordFormData,
|
||||
}
|
||||
);
|
||||
|
||||
let discordData;
|
||||
try {
|
||||
discordData = await discordRes.json();
|
||||
} catch (jsonError) {
|
||||
console.error('Failed to parse Discord response as JSON:', jsonError);
|
||||
return NextResponse.json({ error: 'Failed to parse Discord response' }, { status: 500 });
|
||||
}
|
||||
|
||||
if (!discordRes.ok) {
|
||||
console.error('Discord API Error:', discordData);
|
||||
return NextResponse.json({ error: 'Failed to upload to Discord' }, { status: 500 });
|
||||
}
|
||||
const attachment = discordData.attachments[0];
|
||||
|
||||
if (!attachment) {
|
||||
|
||||
Reference in New Issue
Block a user