Fix: Working Frontend without bugs

This commit is contained in:
Aron Malcher 2024-02-11 19:31:21 +01:00
parent 4e6bd72a21
commit 4e02e51fca
Signed by: aronmal
GPG key ID: 816B7707426FC612
4 changed files with 343 additions and 186 deletions

View file

@ -0,0 +1,111 @@
import { getSession } from "@auth/solid-start";
import { APIEvent } from "@solidjs/start/server/types";
import { eq } from "drizzle-orm";
import moment from "moment-timezone";
import createClient from "openapi-fetch";
import db from "~/drizzle";
import { accounts } from "~/drizzle/schema";
import { authOptions } from "~/server/auth";
import { paths } from "~/types/discord";
type data = {
success: boolean | null;
guild: {
id: string;
name: string | undefined;
icon: string | null | undefined;
channel: string;
channels: {
id: string;
name: string;
}[];
};
tzNames: string[];
};
export const GET = async (
event: APIEvent,
): Promise<
{ success: false; message: string } | (data & { success: true })
> => {
const id = event.params.guildId;
// const id = "598539452343648256";
// const location = useLocation();
if (!event) return { success: false, message: "No request event!" };
const session = await getSession(event.request, authOptions);
if (!session?.user?.id)
return { success: false, message: "No user with id!" };
const { DISCORD_ACCESS_TOKEN } = (
await db
.selectDistinct({ DISCORD_ACCESS_TOKEN: accounts.access_token })
.from(accounts)
.where(eq(accounts.userId, session.user?.id))
.limit(1)
.execute()
)[0];
if (!DISCORD_ACCESS_TOKEN)
return { success: false, message: "No discord access token!" };
const { GET } = createClient<paths>({
baseUrl: "https://discord.com/api/v10",
});
const guildsRequest = await GET("/users/@me/guilds", {
headers: { Authorization: `Bearer ${DISCORD_ACCESS_TOKEN}` },
});
const channelsRequest = await GET("/guilds/{guild_id}/channels", {
params: {
path: {
guild_id: id,
},
},
headers: { Authorization: `Bot ${import.meta.env.VITE_DISCORD_BOT_TOKEN}` },
});
if (guildsRequest.error || channelsRequest.error) {
console.log(guildsRequest.error, channelsRequest.error, location.pathname);
return {
success: false,
message: "Error on one of the discord api requests!",
};
}
const guild = guildsRequest.data?.find((e) => e.id === id);
if (!guild)
return {
success: false,
message: "User is in no such guild with requested id!",
};
if (!(parseInt(guild.permissions) & (1 << 5)))
return {
success: false,
message:
"User is no MANAGE_GUILD permissions on this guild with requested id!",
};
let channels: data["guild"]["channels"] = [];
channelsRequest.data?.forEach((channel) => {
if (channel.type !== 0) return;
channels.push({
id: channel.id,
name: channel.name,
});
});
console.log("done");
return {
success: true,
guild: {
id: guild.id,
name: guild.name,
icon: guild.icon,
// channel: "1162917335275950180",
channel: "",
channels,
},
tzNames: moment.tz.names(),
};
};

View file

@ -0,0 +1,62 @@
import { getSession } from "@auth/solid-start";
import { APIEvent } from "@solidjs/start/server/types";
import { eq } from "drizzle-orm";
import createClient from "openapi-fetch";
import db from "~/drizzle";
import { accounts } from "~/drizzle/schema";
import { authOptions } from "~/server/auth";
import { paths } from "~/types/discord";
type data = {
success: boolean | null;
guilds: {
id: string;
name: string;
icon: string | null | undefined;
}[];
};
export const GET = async (
event: APIEvent,
): Promise<
{ success: false; message: string } | (data & { success: true })
> => {
if (!event) return { success: false, message: "No request event!" };
const session = await getSession(event.request, authOptions);
if (!session?.user?.id)
return { success: false, message: "No user with id!" };
const { DISCORD_ACCESS_TOKEN } = (
await db
.selectDistinct({ DISCORD_ACCESS_TOKEN: accounts.access_token })
.from(accounts)
.where(eq(accounts.userId, session.user?.id))
.limit(1)
.execute()
)[0];
if (!DISCORD_ACCESS_TOKEN)
return { success: false, message: "No discord access token!" };
const { GET } = createClient<paths>({
baseUrl: "https://discord.com/api/v10",
});
const { data: guilds, error } = await GET("/users/@me/guilds", {
headers: { Authorization: `Bearer ${DISCORD_ACCESS_TOKEN}` },
});
console.log("guilds", guilds);
if (error) {
console.log(error);
return { success: false, message: "Error on discord api request!" };
}
return {
success: true,
guilds:
guilds
?.filter((e) => parseInt(e.permissions) & (1 << 5))
.map(({ id, name, icon }) => ({ id, name, icon })) ?? [],
};
};