Optimal way of developing with typescript
Hello!
I've been using sapphire for some days now, and one thing that I'm trying to achieve is to not change anything in the source code of my bot when being done with my developing and deploying to production, or going back to developing after deploying.
I may be completely wrong, but in the documentation, it says that sapphire needs the "main" property of the package.json to point to the main file of my bot. But the issue I'm personally having is that when developing in typescript, I point the "main" property to something like "src/index.ts", but before deploying to production, I manually change that to "dist/index.js".
Is there a way to avoid manually changing the "main" property when developing->deploying?
Solution:Jump to solution
Tsc-watch config is included in sapphire's template bot.
So You can use that instead...
7 Replies
Although bun can directly run typescript files without transpilation/compilation to JS
You have to keep the
main
to dist/index.js
& compile/transpile the files to JS (often known as Build Step)Oh I see, I was trying to avoid the transpilation when developing (I was using ts-node-dev)
I'll mark your answer as the solution in a bit in case someone else knows if any other way is possible
There's a better way
Tag suggestion for @Pipexlul:
TL;DR: Do not use
ts-node
, use tsc-watch
instead.
We very strongly discourage using ts-node
because it was never meant to be used for bots.
ts-node
is designed for REPL
purposes. That's short for Read Eval Print Loop
.
Which means to read some code, dump it in an eval()
statement, print the result, and loop.
A discord bot is not that.
A Discord bot sets up a permanent web socket connection to the Discord server and connects to the rest gateway.
There is read yes, but no eval, no print, and no loop.
So what should you use instead?
The most ideal way is to just use the watch
flag of tsc
(tsc --watch
) and run node dist/index.js
to run your bot, then cancel that process and restart it when you have changes that require restarting.
You would open 2 terminal tabs, 1 in which you run tsc --watch
and another in which you run the bot.
This is, in particular, the most ideal way, because Discord has a limit to the amount of times you can log in with your bot, or register commands, per day.
Constantly logging in over and over again due to an auto-restarting process will get you close to that limit very quickly and once you exceed it, your development will be halted entirely for the current day.
However, this can be quite tedious so a great package to use instead is tsc-watch
.Solution
Tsc-watch config is included in sapphire's template bot.
So You can use that instead
Thank you a lot. That solves my issue 100%
GitHub
examples/examples at main · sapphiredev/examples
Various examples of setting up your bot with the Sapphire Framework - sapphiredev/examples