Command Handling With TypeScript
Hello, I'm having trouble setting up command handling with TypeScript. I'm trying to follow the official guide that is directed towards JavaScript.
What I Have So Far
/src/index.ts
/src/commands/utility/ping.ts
Where I Am Stuck
We recommend attaching a .commands
property to your client instance so that you can access your commands in other files. The rest of the examples in this guide will follow this convention. For TypeScript users, we recommend extending the base Client class to add this property, casting, or augmenting the module type.
I'd like to ask if what I currently have is good so far, and where I go from here. I already tried a couple of different things over the last few days, but I'm starting over and immediately asking here to be sure I understand what I'm doing.6 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!- Node.js: v20.15.1
- discord.js: v14.16.3
Okay, thank you! I explicitly typed the
readyClient
parameter, because otherwise it'd get marked as an error for me, probably due to the way I have set up my tsconfig.json
. I'm just not sure how to attach .commands
to my client instance. To extend the base Client
class, I don't know whether I should augment it or cast it.We highly recommend you extend the
Client
structure properly instead of just attaching custom properties like .commands
to the regular discord.js Client
instance.
- Using typescript, you might want to consider casting or augmenting the module typeYou could also look at how create-discord-bot handles it, the commands are not bound to the client
I personally ended up just exporting a few Collections as my command/event registries:
I can access them in any file I need to with
Thank you for all of your suggestions! I have to put my project on hold for now, but I will definitely continue here once I have the opportunity.
I will definitely document my progress here once I figured it out, perhaps it can help someone else in the future.