Running Discord bot in Next.js App

I'm trying something and I decided to run a Discord bot in my Next.js 15 application and make a system where the bot is started with next.js server I was able to achieve the result as shown in the image (the page is dynamically rendered on each request and shows the current cached users of the client). All I had to do was to assign the client to a global variable and make sure that the bot is started and ready before the requests run. Thus, by using the client in the server-side components (or files) of next.js, I can use ssr without the need for an additional request (usually this is done in client-side to an express backend, a backend where the bot is running and from there you get the necessary informations) Since I can also use client in API routes, I was able to easily create a routes like /api/users/:id. A route that dynamically fetches the user's information with the client and returns it back. All of this seems to be working fine now (when I build the application and run it in production environment, there seems to be no problem), but I still want to make sure that the application is suitable for production environment just in case. Do you think using the client in this way in the server-side part of next.js will cause a problem? has anyone tried it? I set packages like ws that discord.js uses to run only on the server-side. so none of this client related stuff will be available on the client-side
No description
5 Replies
d.js toolkit
d.js toolkit6d ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Amgelo
Amgelo6d ago
I'm not sure how next works specifically but a bot is a stateful app, it needs a persistent connection to the gateway (aka an actual server), it can't be deployed in serverless which is a common pattern for web dev
Leo
Leo6d ago
I don't understand what benefit you are getting from running both in the same application. You said that you can easily create a route that fetches information from the client like "/api/users/:id". Who's information is fetched there? How do you authenticate users over next.js? Do you plan on having the next.js application as a separate user interface to you application or is it more of a monitoring dashboard to visualize information about the discord bot?
benluka.dev
benluka.dev6d ago
It's good, we are running discord bot in nuxt server side. Although it's not really good for scaling, if your site crashes then so does the bot. (If your bot has commands and does stuff on discord) however if you are solely running the bot to get advantage of cache, it's best. No need to create a separate app. But like I said, if other than caching the bot has more features it does and separate from the web app better to run in its own environment
can
canOP6d ago
as i already said, i set it to not connect to the gateway again on every request. it connects once and never again Since I run bot and next.js in the same process, I avoid the hassle of setting up a separate backend. Let's say I want to show the total number of servers of the bot when in the home page. what should I normally do? I will have to open a backend server, create an API service, open a route like /guilds and write an API Route that returns the total number of servers of the bot. I would also need to use fetch in next.js to make a request to the backend to get this count, usually on the server-side, and doing this (unless I use cache) means waiting for the page to load each time to make a fetch request. But, instead, if next.js and bot are in the same process, I can use the total number of servers directly in react through client.guilds.cache, which is already cached, without the need for any additional request next.js never crashes in production environment (edit: of course errors like 500 are returned as a response what I am talking about here are situations such as complete server shutdown)

Did you find this page helpful?