0

I have a JavaScript / TypeScript monorepository generated using something similair to Angular CLI, I have 3 apps: frontend, server and a Discord bot.

The Discord bot is the core of this project, the bot is basically a discord.js class instance (Client), I want both my server and bot project to access the client's data.

I have created a library which exposes a database connection for my server and bot app, my question is what should I do with the class instance? Should I export the class instance in a library and write the logic for it in the bot app and be able to access it in the server? Since the library would export nothing more than

import { Client } from "discord.js"

export const client = new Client()

client.login(process.env.TOKEN)

I was wondering because I could create the logic for the bot inside the library as well but I feel like that is against going against the point of the structure, I would appreciate any feedback

1 Answers1

0

Don't share the instance of the client, have the frontend and server packages import your class and create the instance themselves.

benawad
  • 685
  • 7
  • 17
  • I forgot one important detail, the server and bot need to access the same cached data of the instance, the frontend is not allowed to access this client instance directly, basically the client instance exposes event listeners that I want to write logic for in the bot project and in the server I access the values that are cached in the class and serve that to the frontend through an API –  Apr 13 '20 at 20:13
  • is the server and bot running in the same instance? – benawad Apr 13 '20 at 20:44
  • They are seperate projects but they run at the same time and are related in some way, example: bot listens to message event of the client instance, then caches something inside that instance > server access the instance and sends cached data to the frontend through a route –  Apr 13 '20 at 21:19
  • if they are running in the same instance of node then it's fine to share the instance – benawad Apr 14 '20 at 02:31