use discord.js in app directory
Answered
Chinese perch posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Chinese perchOP
Getting a guild from the Client instance. Tried getServerSideProp and getStaticProp but both of them doesn't work with the app directory. Can someone help me with this?
export default async function GuildPage({
params: { guildId },
}: {
params: { guildId: string };
}) {
const data = await Guild.findOne({ guildId });
if (!data) return <Container>Guild not found!</Container>;
// issue is here:
const guild = discordClient.guilds.cache.get(data.guildId);
console.log(guild);
return <Container>test</Container>;
}
22 Replies
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Chinese perchOP
The "data fetching" docs sounds pretty confusing
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
import { notFound } from "next/navigation";
export default async function GuildPage({
params: { guildId },
}: {
params: { guildId: string };
}) {
const data = await Guild.findOne({ guildId });
if (!data) notFound();
// issue is here:
const guild = discordClient.guilds.cache.get(data.guildId);
console.log(guild);
return <Container>test</Container>;
}
try this
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Chinese perchOP
huh?
// issue is here:
const guild = discordClient.guilds.cache.get(data.guildId);
console.log(guild);
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
what issue
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Chinese perchOP
Module not found: Can't resolve 'zlib-sync'
https://nextjs.org/docs/messages/module-not-found
I went to that link, but it was pretty confusing for me, the code isn't async or anything
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
npm i zlib-sync
?![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Chinese perchOP
it should be installed when i installed discord.js
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
![Image](https://cdn.discordapp.com/attachments/1184089930192932904/1184093120426672158/image.png?ex=658ab7a7&is=657842a7&hm=e84f698cf1543ec4b21e6bb3c5546b0234820f25c4dcf28b3b16170777fc5fd6&)
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Chinese perchOP
./node_modules/zlib-sync/build/Release/zlib_sync.node
Module parse failed: Unexpected character '�' (1:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
I don't use that
there are no uses of websockets in my code
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
im confused why you want that tho... nextjs is desinged for serverless (process only short lived) so i kinda don't see how it would work well
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
you can use the raw api good tho (ie @discordjs/rest)
Answer
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Chinese perchOP
"use server";
import discordClient from "@/client";
import { notFound } from "next/navigation";
export async function GET(
request: Request,
{ params }: { params: { guildId: string } }
) {
const guild = discordClient.guilds.cache.get(params.guildId);
if (!guild) return notFound();
return Response.json({ guildId: params.guildId });
}
Ended up using route, I still have the exact same problem despite removing this from the page i showed earlier
but yes imma look into the api
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
so what exactly do you want with discord.js
just fetching?
if so, i think the discordjs fetch wrapper and unstable_cache may be good (if you want it cached)
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Chinese perchOP
hm ill use @discordjs/rest
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
can i ask how did your adventures go, did things work out well, or more questions to help you support this?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Chinese perchOP
Yep. This way doesn't need the bot to be logged in from the dashboard backend itself, so it's a win I'd say.
client.ts:
in my api route:
client.ts:
import { REST } from "@discordjs/rest";
const discordRest = new REST({ version: "10" }).setToken(
process.env.DISCORD_TOKEN
);
export default discordRest;
in my api route:
const guild = (await discordRest.get(
Routes.guild(params.guildId)
)) as Guild;