Chore: Revert to 'use server' functions
This commit is contained in:
parent
2b9b5198fa
commit
590e7d8265
5 changed files with 171 additions and 203 deletions
|
@ -1,106 +0,0 @@
|
|||
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;
|
||||
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, event.path);
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
guild: {
|
||||
id: guild.id,
|
||||
name: guild.name,
|
||||
icon: guild.icon,
|
||||
// channel: "1162917335275950180",
|
||||
channel: "",
|
||||
channels,
|
||||
},
|
||||
tzNames: moment.tz.names(),
|
||||
};
|
||||
};
|
|
@ -1,60 +0,0 @@
|
|||
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}` },
|
||||
});
|
||||
|
||||
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 })) ?? [],
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue