discord dashboard and discord js combo
What i normally do is, i got a protect middleware that runs on each request.
What does the middleware do?
- it checks if JWT is present in cookie
- if jwt is valid
- gets user id from jwt then make request to database to get document with access token
- then make request to discord api /@me endpoint to validate the access token is still valid or not.
So this happens on every endpoint, now, lets say someone goes to settings of a specific server, i will do following
- protect middleware
- make request to api to check if he has certain roles
- make request to api to show all roles, another request to show all channels
- some database requests to get some data
That's a lot of api calls right? Making the web slow. I thought of using discord js for its cache system (would no need to call dapi for checking roles,fetching roles and channels)
But that means running the server as well as bot in same process. What do you guys do?
25 Replies
- 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!The only part here thats djs is fetching the member. For that you should only need a single api request, the roles will be cached (if you have the Guilds intent)
So yes, you can use djs for that
yeah everything will be cached if i fetch member on start
However, if its the only thing you need full djs for, you're better off just using /core
Pretty sure the roles get sent along when fetching the member
but i thought core has no cache?
It indeed does not
But it has reduced memory usage
so what would be the benefit?
it depends
like in my case
If you would only use djs for that partx you're better off with core
if there's no cache, then core would make requests to api, (thats what i do)
its up to you really, the only significant impact would be memory usage
no no, i am not making it clear.. i instead of calling the endpoints for getting member, then guild roles then guild channels separately want to use cache so djs is the way not core right?
Both, especially if the second one is possible? I thought not possible because two instances handling the same interaction [if any].
I think one main instance for the main bot, one secondary instance for cache for website would be perfect but it is possible?
What do you recommend especially?
how do you access client in another file?
I read you guys saying exporting client from main file is bad and might cause cirular dependency or memory leak i think?
another on same process ofc because of creating api in same bot
how would on a different process also work? ( Might just do that instead of creating an api)
Another machine is also possible 😂 wild
oh api, then api in bot is good.
Not sure if i understand, lets say i got
index.js that runs the bot as well as import the server.js file in index that makes the server run.
If i export a function from index.js and use in server.js that would still be circular dependency?
oh so do all processing of server inside a function?
what if i wanted to split into modules the server.js, app.js and routes and controllers. How would i access client?
wrap all of them in functions and repeat the stuff?
oh okay gotcha thanks , considering using api to send the client in json [to other process or machine] will client have all properties and methods? Cache, getters , setters etc? Or some will get removed due to passing over internet (json stringifyng i think)
bump.
you don't send the client in json, you just use it directly
It was about "exporting variable ، client in this case، to another machine through api"
yeah, exporting the variable, not serializing the client and sending it through rest
though Qjuh mentioned exporting a function, not the variable directly
to avoid circular dependencies
exporting to another machine or different process, not within same process
yes we figured that out it was about same process
.
yes, he answered this:
another on same process ofc because of creating api in same botwith:
You‘d pass the client to the file that’s handling the API. Since you have to import that somewhere so habe it export a function that takes the client as parameter.and then:
how would on a different process also work? Another machine is also possible 😂 wildwith:
And the other two options are possible… through making an API 😉so you don't send the client through the api, that option was when using the same process
yes thats about exporting client to another file within same process through a function
what you'd do is, as Qjuh said, make your bot expose an api that the dashboard would use
I am just curious if i make api to send the client (not export i was using wrong term) yes the point is if i send the client through api to different process or machine
The methods will be lost and properties?
o
right i was just curios thanks
yeah serializing it would be troublesome to say the least, you'd loose a ton of properties and I'm not even sure how you can deserialize it back
yes gotcha thanks mb, i thought sending client through api 💀
oh, true
nerd stuff
Appreciate the help 👊