119 lines
3.4 KiB
TypeScript
119 lines
3.4 KiB
TypeScript
"use server";
|
|
|
|
import { getServerSession } from "next-auth";
|
|
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
|
|
|
export interface DiscordRole {
|
|
id: string;
|
|
name: string;
|
|
color: number;
|
|
position: number;
|
|
}
|
|
|
|
export async function getGuildRoles(): Promise<DiscordRole[]> {
|
|
const token = process.env.DISCORD_BOT_TOKEN;
|
|
const guildId = process.env.DISCORD_GUILD_ID;
|
|
if (!token || !guildId) return [];
|
|
|
|
const session = await getServerSession(authOptions);
|
|
const discordId = (session?.user as any)?.discordId;
|
|
const admins = (process.env.ADMIN_DISCORD_IDS || "").split(",");
|
|
if (!discordId || !admins.includes(discordId)) {
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(`https://discord.com/api/v10/guilds/${guildId}/roles`, {
|
|
headers: { Authorization: `Bot ${token}` },
|
|
next: { revalidate: 60 }, // Cache for 60s
|
|
});
|
|
|
|
if (!res.ok) return [];
|
|
|
|
const roles: any[] = await res.json();
|
|
return roles.map(r => ({
|
|
id: r.id,
|
|
name: r.name,
|
|
color: r.color,
|
|
position: r.position,
|
|
})).sort((a, b) => b.position - a.position);
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function getGuildMemberRoles(discordId: string): Promise<string[]> {
|
|
const token = process.env.DISCORD_BOT_TOKEN;
|
|
const guildId = process.env.DISCORD_GUILD_ID;
|
|
if (!token || !guildId || !discordId) return [];
|
|
|
|
try {
|
|
const res = await fetch(`https://discord.com/api/v10/guilds/${guildId}/members/${discordId}`, {
|
|
headers: { Authorization: `Bot ${token}` },
|
|
cache: "no-store", // Always fresh for access control
|
|
});
|
|
|
|
if (!res.ok) return [];
|
|
|
|
const member = await res.json();
|
|
return member.roles || [];
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function getDiscordMemberProfile(discordId: string) {
|
|
const token = process.env.DISCORD_BOT_TOKEN;
|
|
const guildId = process.env.DISCORD_GUILD_ID;
|
|
if (!token || !guildId || !discordId) return null;
|
|
|
|
try {
|
|
const [memberRes, allRoles] = await Promise.all([
|
|
fetch(`https://discord.com/api/v10/guilds/${guildId}/members/${discordId}`, {
|
|
headers: { Authorization: `Bot ${token}` },
|
|
cache: "no-store",
|
|
}),
|
|
getGuildRoles().catch(() => []) // Fallback to empty array if unauthorized
|
|
]);
|
|
|
|
if (!memberRes.ok) return null;
|
|
const member = await memberRes.json();
|
|
|
|
// Map member role IDs to actual role objects
|
|
const memberRoleIds = member.roles || [];
|
|
const roles = memberRoleIds
|
|
.map((id: string) => allRoles.find(r => r.id === id))
|
|
.filter(Boolean);
|
|
|
|
// Sort roles by position (descending)
|
|
roles.sort((a: any, b: any) => (b?.position || 0) - (a?.position || 0));
|
|
|
|
const user = member.user;
|
|
|
|
let avatarUrl = null;
|
|
if (user.avatar) {
|
|
const ext = user.avatar.startsWith("a_") ? "gif" : "png";
|
|
avatarUrl = `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.${ext}?size=128`;
|
|
}
|
|
|
|
let bannerUrl = null;
|
|
if (user.banner) {
|
|
const ext = user.banner.startsWith("a_") ? "gif" : "png";
|
|
bannerUrl = `https://cdn.discordapp.com/banners/${user.id}/${user.banner}.${ext}?size=512`;
|
|
}
|
|
|
|
return {
|
|
id: user.id,
|
|
username: user.username,
|
|
globalName: user.global_name || null,
|
|
avatarUrl,
|
|
bannerUrl,
|
|
accentColor: user.accent_color || null,
|
|
roles,
|
|
};
|
|
} catch (e) {
|
|
console.error("Failed to fetch member profile", e);
|
|
return null;
|
|
}
|
|
}
|