Run bot without building to js and use ts-node or other alternatives instead.
Is there a way to achieve the title? Currently I need to build and then run the js file using the command:
Solution:Jump to solution
If you want to test the bot, then
tsc-watch
is good.
Or you can remove typescript & code directly in js
Or you can use bun runtime to start your typescript based bot directly (you will need to refactor your code so that it runs on bun)...10 Replies
Solution
If you want to test the bot, then
tsc-watch
is good.
Or you can remove typescript & code directly in js
Or you can use bun runtime to start your typescript based bot directly (you will need to refactor your code so that it runs on bun)I would prefer to use typescript
and i don't really want to use another runtime
tsc-watch
& tsup
have an option called onSuccess <do something>
So you can leverage that.
Or create a new script in package.json:
https://github.com/sapphiredev/examples/blob/main/examples/with-typescript-starter/package.json#L35
Directly from Sapphire's examples repo
GitHub
examples/examples/with-typescript-starter/package.json at main · sa...
Various examples of setting up your bot with the Sapphire Framework - sapphiredev/examples
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 websocket 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 login 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
.Slightly off topic but is there an equivalent to tsc-watch for swc?
Install chokidar and add "-w" for watching
@swc/cli – SWC
SWC is an extensible Rust-based platform for the next generation of fast developer tools. It's used by tools like Next.js, Parcel, and Deno, as well as companies like Vercel, ByteDance, Tencent, Shopify, and more.
Thank you. I meant the way you can use
--onSuccess
, it doesn't seem to exist for swc without watching both src and dist with separate scripts.Ah my bad. Yeah I'm not sure if that exists