지금까지 내용 커밋
This commit is contained in:
34
page/src/app/api/servers/route.ts
Normal file
34
page/src/app/api/servers/route.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { Redis } from "@/lib/Redis";
|
||||
import { authOptions } from "../auth/[...nextauth]/route";
|
||||
|
||||
export async function GET() {
|
||||
const session = await getServerSession(authOptions) as any;
|
||||
|
||||
if (!session || !session.accessToken) {
|
||||
return NextResponse.json({ error: "인증되지 않았습니다." }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 디스코드 API에서 유저가 속한 서버 목록 가져오기
|
||||
const userGuildsRes = await fetch("https://discord.com/api/users/@me/guilds", {
|
||||
headers: { Authorization: `Bearer ${session.accessToken}` },
|
||||
});
|
||||
const userGuilds = await userGuildsRes.json();
|
||||
|
||||
// 2. Redis에서 봇이 속한 서버 목록(화이트리스트) 가져오기
|
||||
const botGuildsData = await Redis.get("bot-guilds");
|
||||
const botGuildIds: string[] = botGuildsData ? JSON.parse(botGuildsData) : [];
|
||||
|
||||
// 3. 🌟 두 목록을 비교해서 봇이 있는 서버만 필터링!
|
||||
const filteredGuilds = userGuilds.filter((guild: any) =>
|
||||
botGuildIds.includes(guild.id)
|
||||
);
|
||||
|
||||
return NextResponse.json(filteredGuilds);
|
||||
} catch (error) {
|
||||
console.error("서버 필터링 에러:", error);
|
||||
return NextResponse.json({ error: "서버 목록을 가져오지 못했습니다." }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user