Files
buzz/app/api/discord/users/api.ts
T
dmgnr 4559f4693c
Build and Push Image / build-backend (push) Successful in 2m13s
Build and Push Image / build-frontend (push) Successful in 4m9s
Build and Push Image / push (push) Successful in 4m21s
refactor(host): migrate from docker swarm to kubernetes
2026-06-19 18:18:17 +00:00

77 lines
1.9 KiB
TypeScript

"use server";
import { env } from "node:process";
import Snowflake, { GatewayIntents } from "@sfjs/snowflake";
import { forbidden } from "next/navigation";
import { adminCheck } from "@/lib/auth";
import { redis } from "@/lib/db/redis";
const bot = new Snowflake({
token: env.DISCORD_BOT_TOKEN || "",
intents: [GatewayIntents.GUILDS, GatewayIntents.GUILD_MEMBERS],
});
type User = {
nick: string | null;
user: {
global_name: string | null;
username: string;
id: string;
};
};
bot.on("guildMemberAdd", async (member: User) => {
const cached: { username: string; uid: string; display: string }[] =
JSON.parse((await redis!.get("userlist")) ?? "[]");
if (cached.some((m) => m.uid === member.user.id))
cached.push({
username: member.user.username,
display: member.nick ?? member.user.global_name ?? member.user.username,
uid: member.user.id,
});
redis!.set("userlist", JSON.stringify(cached));
});
export async function getDiscordUsers() {
if (!(await adminCheck())) forbidden();
const members: { username: string; uid: string; display: string }[] = [];
let after = "0";
const cached = await redis!.get("userlist");
if (cached)
return JSON.parse(cached) as {
username: string;
uid: string;
display: string;
}[];
while (true) {
// fetch up to 1000 members after the last user id
const batch: User[] = await bot.guilds[env.DISCORD_GUILD_ID!].members({
limit: 1000,
after,
});
console.log(batch);
if (!batch.length) break;
members.push(
...batch.map((e) => ({
username: e.user.username,
display: e.nick ?? e.user.global_name ?? e.user.username,
uid: e.user.id,
})),
);
// discord pagination cursor
after = batch[batch.length - 1].user.id;
// stop if this was the final page
if (batch.length < 1000) break;
}
redis!.set("userlist", JSON.stringify(members));
return members;
}