feat(form) : force user to join discord server
This commit is contained in:
+40
-3
@@ -23,18 +23,55 @@ 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;
|
||||
|
||||
try {
|
||||
const res = await fetch(
|
||||
`https://discord.com/api/v10/guilds/${encodeURIComponent(guildId)}/members/${encodeURIComponent(discordId)}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
Authorization: `Bot ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ access_token: accessToken }),
|
||||
signal: AbortSignal.timeout(5000),
|
||||
},
|
||||
);
|
||||
|
||||
if (res.status === 201 || res.status === 204) return true;
|
||||
|
||||
const body = await res.text().catch(() => "");
|
||||
console.error(
|
||||
`Failed to add Discord member ${discordId} to guild ${guildId}: ${res.status}${body ? ` ${body.slice(0, 300)}` : ""}`,
|
||||
);
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error(`Failed to add Discord member ${discordId} to guild ${guildId}`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGuildRolesInternal(): Promise<DiscordRole[]> {
|
||||
const cached = getCached(rolesCache.entry);
|
||||
if (cached) return cached;
|
||||
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const guildId = process.env.DISCORD_GUILD_ID;
|
||||
const guildId = getDiscordGuildId();
|
||||
if (!token || !guildId) return [];
|
||||
|
||||
try {
|
||||
@@ -69,7 +106,7 @@ export async function getGuildRolesInternal(): Promise<DiscordRole[]> {
|
||||
|
||||
export async function getGuildMemberRoles(discordId: string): Promise<string[]> {
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const guildId = process.env.DISCORD_GUILD_ID;
|
||||
const guildId = getDiscordGuildId();
|
||||
if (!token || !guildId || !discordId) return [];
|
||||
|
||||
try {
|
||||
@@ -93,7 +130,7 @@ export async function getDiscordMemberProfile(discordId: string): Promise<Discor
|
||||
if (cached) return cached;
|
||||
|
||||
const token = process.env.DISCORD_BOT_TOKEN;
|
||||
const guildId = process.env.DISCORD_GUILD_ID;
|
||||
const guildId = getDiscordGuildId();
|
||||
if (!token || !guildId || !discordId) return null;
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user