mirror of
https://github.com/xHyroM/roles-bot.git
synced 2024-11-12 20:18:06 +01:00
feat: prepare for dynamic rendering
This commit is contained in:
parent
b7dd3bf3ef
commit
92d0c39db2
6 changed files with 121 additions and 14 deletions
|
@ -1,5 +1,6 @@
|
||||||
import Discord from "@auth/core/providers/discord";
|
import Discord from "@auth/core/providers/discord";
|
||||||
import { defineConfig } from "auth-astro";
|
import { defineConfig } from "auth-astro";
|
||||||
|
import type { User } from "~/env";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
providers: [
|
providers: [
|
||||||
|
@ -15,6 +16,9 @@ export default defineConfig({
|
||||||
if (account && profile) {
|
if (account && profile) {
|
||||||
token.accessToken = account.access_token;
|
token.accessToken = account.access_token;
|
||||||
token.id = profile.id;
|
token.id = profile.id;
|
||||||
|
|
||||||
|
token.name = profile.username as string;
|
||||||
|
token.global_name = profile.global_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return token;
|
return token;
|
||||||
|
@ -22,15 +26,20 @@ export default defineConfig({
|
||||||
async session({ session, token }) {
|
async session({ session, token }) {
|
||||||
if (session.user) {
|
if (session.user) {
|
||||||
session.user.id = token.id as string;
|
session.user.id = token.id as string;
|
||||||
|
(session.user as unknown as User).global_name =
|
||||||
|
token.global_name as string;
|
||||||
|
|
||||||
const guilds = await fetch("https://discord.com/api/users/@me/guilds", {
|
const guilds = await fetch(
|
||||||
headers: {
|
"https://discord.com/api/v10/users/@me/guilds",
|
||||||
Authorization: `Bearer ${token.accessToken as string}`,
|
{
|
||||||
"Cache-Control": "max-age=300",
|
headers: {
|
||||||
},
|
Authorization: `Bearer ${token.accessToken as string}`,
|
||||||
});
|
"Cache-Control": "max-age=300",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
session.user.guilds = await guilds.json();
|
(session.user as unknown as User).guilds = await guilds.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
|
|
15
apps/website/src/env.d.ts
vendored
15
apps/website/src/env.d.ts
vendored
|
@ -3,9 +3,14 @@
|
||||||
import type { User as AuthCoreUser } from "@auth/core/types";
|
import type { User as AuthCoreUser } from "@auth/core/types";
|
||||||
|
|
||||||
export type User = AuthCoreUser & {
|
export type User = AuthCoreUser & {
|
||||||
guilds: {
|
global_name: string;
|
||||||
id: string;
|
guilds: Guild[];
|
||||||
name: string;
|
|
||||||
icon: string;
|
|
||||||
}[];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface Guild {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
icon: string;
|
||||||
|
permissions: string;
|
||||||
|
owner: boolean;
|
||||||
|
}
|
||||||
|
|
43
apps/website/src/lib/guilds.ts
Normal file
43
apps/website/src/lib/guilds.ts
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import type { Guild } from "~/env";
|
||||||
|
|
||||||
|
// Checks if user has AMDINISTRATOR permissions in the guild
|
||||||
|
export async function filterUserGuilds(guilds: Guild[]): Promise<
|
||||||
|
{
|
||||||
|
mutual: boolean;
|
||||||
|
guild: Guild;
|
||||||
|
}[]
|
||||||
|
> {
|
||||||
|
const filtered = guilds
|
||||||
|
.filter((g) => (BigInt(g.permissions) & 0x8n) == 0x8n)
|
||||||
|
.sort((a, b) => Number(b.owner) - Number(a.owner));
|
||||||
|
|
||||||
|
const result: {
|
||||||
|
mutual: boolean;
|
||||||
|
guild: Guild;
|
||||||
|
}[] = [];
|
||||||
|
|
||||||
|
for (const guild of filtered) {
|
||||||
|
const mutual = await isMutualGuild(guild);
|
||||||
|
|
||||||
|
result.push({ mutual, guild });
|
||||||
|
}
|
||||||
|
|
||||||
|
result.sort((a, b) => Number(b.mutual) - Number(a.mutual));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function isMutualGuild(guild: Guild): Promise<boolean> {
|
||||||
|
const res = await fetch(
|
||||||
|
`https://discord.com/api/v10/guilds/${guild.id}/members/${
|
||||||
|
import.meta.env.DISCORD_CLIENT_ID
|
||||||
|
}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bot ${import.meta.env.DISCORD_BOT_TOKEN}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return res.ok;
|
||||||
|
}
|
23
apps/website/src/pages/api/user/guilds.ts
Normal file
23
apps/website/src/pages/api/user/guilds.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import type { APIRoute } from "astro";
|
||||||
|
import { getSession } from "auth-astro/server";
|
||||||
|
import type { User } from "~/env";
|
||||||
|
|
||||||
|
export const GET: APIRoute = async ({ request }) => {
|
||||||
|
const session = await getSession(request);
|
||||||
|
if (!session || !session.user) {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = session.user as User;
|
||||||
|
|
||||||
|
return Response.json(
|
||||||
|
user.guilds.map((g) => ({
|
||||||
|
id: g.id,
|
||||||
|
name: g.name,
|
||||||
|
owner: g.owner,
|
||||||
|
permissions: g.permissions,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
};
|
21
apps/website/src/pages/api/user/index.ts
Normal file
21
apps/website/src/pages/api/user/index.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import type { APIRoute } from "astro";
|
||||||
|
import { getSession } from "auth-astro/server";
|
||||||
|
import type { User } from "~/env";
|
||||||
|
|
||||||
|
export const GET: APIRoute = async ({ request }) => {
|
||||||
|
const session = await getSession(request);
|
||||||
|
if (!session || !session.user) {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 401,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = session.user as User;
|
||||||
|
|
||||||
|
return Response.json({
|
||||||
|
id: user.id,
|
||||||
|
username: user.name,
|
||||||
|
global_name: user.global_name,
|
||||||
|
avatar_url: user.image,
|
||||||
|
});
|
||||||
|
};
|
|
@ -1,5 +1,6 @@
|
||||||
---
|
---
|
||||||
import type { User } from "~/env";
|
import type { User } from "~/env";
|
||||||
|
import { filterUserGuilds } from "~/lib/guilds";
|
||||||
import { getSession } from "auth-astro/server";
|
import { getSession } from "auth-astro/server";
|
||||||
import Layout from "~/layouts/Layout.astro";
|
import Layout from "~/layouts/Layout.astro";
|
||||||
import Container from "~/components/Container.astro";
|
import Container from "~/components/Container.astro";
|
||||||
|
@ -11,6 +12,7 @@ if (!session || !session.user) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = session.user as User;
|
const user = session.user as User;
|
||||||
|
const guilds = await filterUserGuilds(user.guilds);
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout>
|
<Layout>
|
||||||
|
@ -33,8 +35,12 @@ const user = session.user as User;
|
||||||
class="flex flex-wrap items-center justify-center gap-12 pb-4 text-white"
|
class="flex flex-wrap items-center justify-center gap-12 pb-4 text-white"
|
||||||
>
|
>
|
||||||
{
|
{
|
||||||
user.guilds.map((guild) => (
|
guilds.map(({ guild, mutual }) => (
|
||||||
<section class="flex min-h-max w-80 justify-between rounded-md border-[1px] border-neutral-800 bg-dark-100 p-6 md:w-96">
|
<section
|
||||||
|
class={`flex min-h-max w-80 justify-between rounded-md border-[1px] border-neutral-800 bg-dark-100 p-6 md:w-96 ${
|
||||||
|
!mutual && "brightness-50"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
<div class="flex flex-row items-center">
|
<div class="flex flex-row items-center">
|
||||||
<Image
|
<Image
|
||||||
src={`https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.webp`}
|
src={`https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.webp`}
|
||||||
|
|
Loading…
Reference in a new issue