Handling commands with promises

Hi! So I have been working on this problem for a bit now. I've slowly solved most of the issues I've run into, but this one has me stumped. I know how to handle promises normally, but I cant seem to be able to make it work when loading commands. Here is a little explanation of the command im making. Basically, In my server we have a command to create characters. That command seems to be working find, but then I made a command that retrieves that character info. In order to avoid having any spelling issues or dealing with case insensitivity What I did was loaded all of the characters in the db using prismaORM and mapped the names so they can be passed into the setChoices method in the EmbedBuilder class. However because this requires async/await it isn't quite working right. Below is my code and a screenshot of the error I'm getting. Thank you in advance for the help on this! There actually isnt an error, but my condition in index.js is saying "Command with name [] not found. Even though the deploy-commands.js is loading 4 commands (and thats all i have so far.)
62 Replies
d.js toolkit
d.js toolkit5mo ago
- 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!
treble/luna
treble/luna5mo ago
seems like your command isnt being pushed into client.commands then you'll have to log to see what is being pushed
IncendiumSkye
IncendiumSkyeOP5mo ago
doesnt seem to be the case, its pushing exactly what Im expecting it to
treble/luna
treble/luna5mo ago
if it was this wouldnt be your issue
IncendiumSkye
IncendiumSkyeOP5mo ago
No description
IncendiumSkye
IncendiumSkyeOP5mo ago
this is part of the log before the commands are deployed its in there which is why im confused
treble/luna
treble/luna5mo ago
deploying your commands =/= populating your client.commands client.commands doesnt exist. its a custom property which you have to populate with your commands they are not automatically added also why are you setting the avatar every time your bot logs in is that ai code
IncendiumSkye
IncendiumSkyeOP5mo ago
ah i see and some of it, not all. I do see that the name of the command when its loading client.commands.set is undefined index.js specifically is directly from the docs except the setAvatar. I did add that myself. I didn't know client.once is called every time. Thats on me, ive only been looking into the docs a couple days good catch on the avatar. I must have put that there yesterday when I got a little frustrated. Moved that right below where client was declared instead
Amgelo
Amgelo5mo ago
.once is called only the first time the event is called, and in the case of ClientReady it's only called once so it doesn't matter much I think what he meant is that you don't need to set your avatar on every login, you should only need to do it once via the portal unless the logo changes for some reason
IncendiumSkye
IncendiumSkyeOP5mo ago
understood. Not sure why the other person said client.commands doesnt exist this is directly from the docs
No description
treble/luna
treble/luna5mo ago
im saying it doesnt exist by default you create it and thus you are responsible to populate it its not bound to the commands you deploy
IncendiumSkye
IncendiumSkyeOP5mo ago
its in the index.js. Im not sure what you mean
Amgelo
Amgelo5mo ago
it doesn't exist as in, it's not something managed by djs it's a property you create as the user
treble/luna
treble/luna5mo ago
your issue is with you populating your client.commands
Amgelo
Amgelo5mo ago
you could perfectly name it "incendiumsCommands" for instance and it'd work just the same
treble/luna
treble/luna5mo ago
which you seem to be confusing with deploying your command might be deployed but its not in your client.commands
IncendiumSkye
IncendiumSkyeOP5mo ago
gotcha so it would be in a configuration file probably right? okay so heres one small isue with this line of thinking
treble/luna
treble/luna5mo ago
its right here
IncendiumSkye
IncendiumSkyeOP5mo ago
my other commands work
treble/luna
treble/luna5mo ago
then you have to debug and figure out why that command isnt being set into client.commands
IncendiumSkye
IncendiumSkyeOP5mo ago
helpful...
treble/luna
treble/luna5mo ago
debugging is core js
IncendiumSkye
IncendiumSkyeOP5mo ago
I dont mean to be rude, but I have been debugging. If I hadnt been trying for a while now I wouldnt have posted it in here
treble/luna
treble/luna5mo ago
then log to see what you are setting into client.commands log the files you are loading
IncendiumSkye
IncendiumSkyeOP5mo ago
It is undefined when loading into client.commands. i could use some help with where to go from there
treble/luna
treble/luna5mo ago
log the files you are requiring and see if your file is in it
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Amgelo
Amgelo5mo ago
pretty sure the error should be caused by the method returning a promise, which isn't the data it expects
Amgelo
Amgelo5mo ago
every async method returns a promise, that's how they work
No description
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/luna5mo ago
their cursed ai handler accounts for that.
d.js docs
d.js docs5mo ago
:guide: Slash Commands: Autocomplete read more
Amgelo
Amgelo5mo ago
oh wait yeah, should've looked at the handler first
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/luna5mo ago
indeed
Amgelo
Amgelo5mo ago
using autocomplete would be much easier imo you should follow the guide gwapes linked
d.js docs
d.js docs5mo ago
:guide: Creating Your Bot: Registering slash commands read more
treble/luna
treble/luna5mo ago
and use this to register
IncendiumSkye
IncendiumSkyeOP5mo ago
So the main issue im noticing which is why I it here is that the command being loaded is a promise. That is what is stopping the command from being properly loaded. I had figured that part out. I was wondering if anyone here knew of a good way to handle that situation
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
IncendiumSkye
IncendiumSkyeOP5mo ago
the client export was a previous issue i hadnt removed, my bad
treble/luna
treble/luna5mo ago
by not having promises. There is no reason to have a promise in your data
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/luna5mo ago
slash commands only have to deployed once
IncendiumSkye
IncendiumSkyeOP5mo ago
there is when you are loading from a database. Prisma takes a moment to load all the data i need.
treble/luna
treble/luna5mo ago
if you have dynamic choices, use autocomplete Then you use autocomplete
Amgelo
Amgelo5mo ago
using autocomplete would also allow you to change your available options in runtime
IncendiumSkye
IncendiumSkyeOP5mo ago
ill look into that! Sorry I was jumping back and forth Thought choices was the only option for a choosable string option
treble/luna
treble/luna5mo ago
do mind the limit is 25 options, so youll have to slice your autocomplete options
Amgelo
Amgelo5mo ago
well autocomplete works more as a suggestion thing you should still validate if what the user submitted is valid input
IncendiumSkye
IncendiumSkyeOP5mo ago
so it would be able to pick from my list of characters from the database? This feels like it might lead to the same issue
treble/luna
treble/luna5mo ago
no autocomplete is at runtime deploying is separate and only once its a separate script at least, meant to be
Amgelo
Amgelo5mo ago
autocomplete makes you receive an interaction when the user types in the option, and you can respond to the interaction which will set what options are shown to the user further details are in the guide I'd suggest using some kind of cache though given interactions expire after 3 seconds, and you can't defer autocompletes
IncendiumSkye
IncendiumSkyeOP5mo ago
Hmmm I dont think this will accomplish what im going for. Well, thank you all for your help. I'll keep looking then. I feel like I'm very close to getting this to work, just need to find a way to resolve the promise before trying to add the command.
treble/luna
treble/luna5mo ago
remove the promise remove the options autocomplete handles that no need for a db call when deploying
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Amgelo
Amgelo5mo ago
with your current method you'd have to redeploy every time your options change
treble/luna
treble/luna5mo ago
the only thing you need is a .setAutocomplete(true) on your option and then handle that interaction and respond with your choices
IncendiumSkye
IncendiumSkyeOP5mo ago
Ah I see, that gives me a little more insight on the proces then. It preloads data and doesnt get updated as I update the database.....dang it. Welp, it was worth a try> i will look into the autocomplete approach. Thanks yall!
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server