From a20b8008f4947b4814e7ddd5c7f9e46e6f52b954 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Wed, 17 Apr 2024 23:47:42 -0400 Subject: [PATCH] Server embeds --- .../app/(pages)/server/[[...slug]]/page.tsx | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Frontend/src/app/(pages)/server/[[...slug]]/page.tsx b/Frontend/src/app/(pages)/server/[[...slug]]/page.tsx index 6a71944..5738633 100644 --- a/Frontend/src/app/(pages)/server/[[...slug]]/page.tsx +++ b/Frontend/src/app/(pages)/server/[[...slug]]/page.tsx @@ -1,3 +1,4 @@ +import Embed from "@/components/embed"; import ServerResult from "@/components/server/server-result"; import ServerSearch from "@/components/server/server-search"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; @@ -87,6 +88,41 @@ const ServerPage = async ({ params }: PageProps): Promise => { export const generateMetadata = async ({ params, }: PageProps): Promise => { + const platform: string | undefined = params.slug?.[0]; // The platform to search for + const hostname: string | undefined = params.slug?.[1]; // The hostname of the server to search for + + // Try and get the server to display + if (platform && hostname) { + try { + const serverPlatform: ServerPlatform = platform as ServerPlatform; + const server: + | CachedJavaMinecraftServer + | CachedBedrockMinecraftServer = await getMinecraftServer( + serverPlatform, + hostname + ); // Get the server to embed + return Embed({ + title: `${serverPlatform} Server: ${server.hostname}`, + description: `There are ${server.players.online}/${server.players.max} playing here!\n\nClick to view data about this server.`, + thumbnail: (server as CachedJavaMinecraftServer).favicon?.url, + }); + } catch (err) { + const code: number = (err as RestfulMCAPIError).code; // Get the error status code + if (code === 400) { + return Embed({ + title: "Invalid Hostname", + color: "#EB4034", + description: `The hostname ${hostname} is invalid.`, + }); + } else if (code === 404) { + return Embed({ + title: "Server Not Found", + color: "#EB4034", + description: `The server ${hostname} was not found.`, + }); + } + } + } return {}; };