chore(config): centralize feature settings
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { config } from "@/lib/config";
|
||||
|
||||
const DISCORD_WEBHOOK_HOSTS = new Set([
|
||||
"discord.com",
|
||||
"canary.discord.com",
|
||||
@@ -31,7 +33,7 @@ export function previewWebhookTemplate(
|
||||
if (key === "username" || key === "displayname") return "Example User";
|
||||
if (key === "id") return "123456789";
|
||||
if (key === "link") {
|
||||
return "https://erika.sudloh.com/admin/form/example-form/result/example-submission";
|
||||
return `${config.site.url}/admin/form/example-form/result/example-submission`;
|
||||
}
|
||||
if (key === "update") return "Question 1: Previous answer → Example answer";
|
||||
if (/^\d+$/.test(key)) return `Example answer ${key}`;
|
||||
|
||||
+9
-20
@@ -3,6 +3,7 @@ import {
|
||||
discordAvatarDecorationUrl,
|
||||
discordGuildTagBadgeUrl,
|
||||
} from "@/lib/discord/profile-appearance";
|
||||
import { getDiscordServerConfig } from "@/lib/config/server";
|
||||
|
||||
export interface DiscordRole {
|
||||
id: string;
|
||||
@@ -31,23 +32,16 @@ interface CacheEntry<T> {
|
||||
const rolesCache: { entry: CacheEntry<DiscordRole[]> | null } = { entry: null };
|
||||
const profileCache = new Map<string, CacheEntry<DiscordMemberProfile>>();
|
||||
const CACHE_TTL = 60_000;
|
||||
const DEFAULT_DISCORD_GUILD_ID = "1302977523310923847";
|
||||
|
||||
function getCached<T>(entry: CacheEntry<T> | null | undefined): T | null {
|
||||
if (entry && Date.now() < entry.expiresAt) return entry.data;
|
||||
return null;
|
||||
}
|
||||
|
||||
function getDiscordGuildId() {
|
||||
return process.env.DISCORD_GUILD_ID || DEFAULT_DISCORD_GUILD_ID;
|
||||
}
|
||||
|
||||
export async function addDiscordGuildMember(discordId: string, accessToken: string): Promise<boolean> {
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const guildId = getDiscordGuildId();
|
||||
if (!token || !guildId || !discordId || !accessToken) return false;
|
||||
if (!discordId || !accessToken) return false;
|
||||
|
||||
try {
|
||||
const { token, guildId } = getDiscordServerConfig();
|
||||
const res = await fetch(
|
||||
`https://discord.com/api/v10/guilds/${encodeURIComponent(guildId)}/members/${encodeURIComponent(discordId)}`,
|
||||
{
|
||||
@@ -69,7 +63,7 @@ export async function addDiscordGuildMember(discordId: string, accessToken: stri
|
||||
);
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error(`Failed to add Discord member ${discordId} to guild ${guildId}`, error);
|
||||
console.error(`Failed to add Discord member ${discordId}`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -78,11 +72,8 @@ export async function getGuildRolesInternal(): Promise<DiscordRole[]> {
|
||||
const cached = getCached(rolesCache.entry);
|
||||
if (cached) return cached;
|
||||
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const guildId = getDiscordGuildId();
|
||||
if (!token || !guildId) return [];
|
||||
|
||||
try {
|
||||
const { token, guildId } = getDiscordServerConfig();
|
||||
const res = await fetch(`https://discord.com/api/v10/guilds/${guildId}/roles`, {
|
||||
headers: { Authorization: `Bot ${token}` },
|
||||
signal: AbortSignal.timeout(5000),
|
||||
@@ -113,11 +104,10 @@ export async function getGuildRolesInternal(): Promise<DiscordRole[]> {
|
||||
}
|
||||
|
||||
export async function getGuildMemberRoles(discordId: string): Promise<string[]> {
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const guildId = getDiscordGuildId();
|
||||
if (!token || !guildId || !discordId) return [];
|
||||
if (!discordId) return [];
|
||||
|
||||
try {
|
||||
const { token, guildId } = getDiscordServerConfig();
|
||||
const res = await fetch(`https://discord.com/api/v10/guilds/${guildId}/members/${discordId}`, {
|
||||
headers: { Authorization: `Bot ${token}` },
|
||||
cache: "no-store",
|
||||
@@ -137,11 +127,10 @@ export async function getDiscordMemberProfile(discordId: string): Promise<Discor
|
||||
const cached = getCached(profileCache.get(discordId));
|
||||
if (cached) return cached;
|
||||
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const guildId = getDiscordGuildId();
|
||||
if (!token || !guildId || !discordId) return null;
|
||||
if (!discordId) return null;
|
||||
|
||||
try {
|
||||
const { token, guildId } = getDiscordServerConfig();
|
||||
const [memberRes, userRes, allRoles] = await Promise.all([
|
||||
fetch(`https://discord.com/api/v10/guilds/${guildId}/members/${discordId}`, {
|
||||
headers: { Authorization: `Bot ${token}` },
|
||||
|
||||
Reference in New Issue
Block a user