Custom slash commands registration
Infos: https://discord.com/channels/737141877803057244/737142209639350343/1240606406290178078µ
Hey, so im doing this to create some custom slash commands.
So the question is, does sapphire, have kind of collection or something i can add the command to it? so it don't get removed
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
115 Replies
so it don't get removedit does not get removed
it does
If you're using BulkOverwrite, yes
If you use Overwrite or any other strategy, it shouldn't
im talking about this command, btw
not the command file
you should not create commands like that
that is anti-sapphire
Im not registering the other commands like this.
Undied please, there are reasons why this is made, namely for hoisting custom commands/tags as guild commands
(Which is a feature I'd like in one of my bots, which makes me understand the need)
exactly, thanks for understanding
if a command is created in a guild, it shouldnt be removed after logout right?
it's for tags, every tag i want it as a comand
it's not this the problem, the problem is the command created by me is not gonna be saved to the collection or the thing that use sapphire to store commands
What I said about the strategy is because it should in theory not remove the commands unless you're using BulkOverwrite, which replaces the commands with those Sapphire can read
Oh, that
store in a database
Why store in a database when Discord gives it to us + we fetch it anyways
https://github.com/SpecteraLabs/Tagger/blob/main/src/commands/tag.ts#L91
something like this
Gimme a sec, Youssef
well i know, i want to see if there is another way to this with sapphire
good lord i didnt delete that repo
Take your time, and thank you so much for helping
the tag response, kyra
Check this in your bot, @youssef
s!ev client.application.commands.cache;
Output: ⏱ 502.26μs
alright give me a seccond
It should contain all registered commands for the client
i get it
nvm
The reason why this would work, Undied, is because Youssef is registering in the client scope, see ^
i forgot the way djs cache actually worked
hmm i tried creating a command, restarting the bot , run this
it doesn't contain the created command
although when i try it after creating the command exacly (so no restart), it has the command
Does the custom command exist on Discord when you try /asana?
if i remember, the djs cache adds guild commands when someone runs it, i might be totally wrong as well
Undied, those aren't guild commands.
yes when i don't restart, it does exist
the /asana
is a guild one kyra
Not a guild command, see the code ^
, interaction?.guild.id
Ok, sorry, I stand corrected
I could barely see that, the font is too small
kyra can we atleast force djs cache to add something to it?
Well, for guilds, you'd have to fetch with the guild
Considering that information, you'll have to store the IDs in the database 😅
kyra, is there way to add the command when i create it to the "collection or the thing" that sapphire use
that is discord.js' cache and i'm afraid the guild commands are added only when someone executes it
the same way members are cached
and after that, the problem is more saphire side
discord.js side*
no?
why is it djs side?
sapphire doesnt have this "collection" or cache
it is purely djs
the command is created correcly, it's when the bot got resttarted
For fetching guild commands, you need to pass the guild ID to
ApplicationCommandManager#fetch
discord,js cache doesnt work the way you think
the only cache that sapphire has is a list of commands from the commands files and it matches ids from discordjs to them
Sapphire doesn't know that a command was created under a guild
@youssef
if only there was a way you could force the cache
What im saying is, the command is created correcly, it's when the bot got restarted and saphire gonna deploy the command it will not find this command
Youssef, you're not being misunderstood
Thing is, this is a Discord limitation, not a Discord.js or a Sapphire one
even if you do it without sapphire and only djs, it will be the same limitation
so what should i do exactly?
There are 2 endpoints for fetching your bot's registered commands:
- https://discord.com/developers/docs/interactions/application-commands#get-global-application-commands
- https://discord.com/developers/docs/interactions/application-commands#get-guild-application-commands
1) force feed the cache by fetching guild commands on startup
There's no such endpoint that returns global and ALL guild commands
Neither Discord.js nor Sapphire knows which guilds have custom commands
if i register the command globally, will it work?
Yes
yes
But it'll be available in all guilds, idk if that's desirable for you
Question, why do you need that cache populated?
no point but thats what they asked for
More often than not, bots should not care about whether a command exists in their cache, Discord will send you all the information you need to locate the command
hmm, i just want the command to stay in the discord
it does
Populating that cache will do nothing, that's the thing
Sapphire won't delete it, nor will Discord.js
it does not when i restart the bot, it get removed
@youssef you have to do this, you are forced to; there is no other way round
it doesnt get removed, the cache gets refreshed
bro it got deleted when i restarted the bot
Register the command, restart the bot, restart Discord, tell me if it exists in the UI
from the discord app?
alright
yes
it shouldnt, right?
it got removed after restaring
youssef would you mind if i take a look on the server?
wait im gonna share the video
Video Upload & Annotation - Gemoo
Record you and your screen to clarify your points. Get vidoes organized as mind map, calendar, or any layout. And share it with a link to boost your productivity.
In that case, @vladdy you know about this better than me (since you wrote the registries), is there any code removing registered application commands on a guild level?
Solution
Only if you use bulk overwrite
Otherwise we do not delete commands, never have, never will
That's the first thing I said, here ^
So...
Yeah well that's the only place where we actually may delete commands
Youssef, are you using BulkOverwrite?
I don't think so
that's my client class
BulkOverwrite isn't written there
Wait what's the issue
I'm omw from an exam so bare with stupid questions
Youssef is registering a guild command manually from a command
But after restarting the bot, the guild command gets deleted
Code here for the registering, which is inside
/tag
's chatInputRun
method ^That's... Impossible unless they've set the default behavior to bulk before, then removed it, and tsc didn't update
Assuming ts code of course
It's TS, yes
or they have a listener that deletes commands
im checking the files
just a sec
Actually yes 🥲
removing it, should fix the problem i guess. what should behaviour should i put instead?
.Overwrite
Overwrite is fine, yes
Overwrite or VerboseOverwrite
Or none
Default is overwrite anyways
Alright i will try
edit: worked!
i can listen to the interactionCreate?, because for the custom command created, i need to reply but it doesnt have file so i'll reply in the code (getting data from the database)
@kyra 🩵🩷🤍🩷🩵?
Yes
But uh
I think we emit an event when there is no match
You should listen to that to ensure that not 2 run side by side
That'll get very fucky
I can't check rn I'm about to drive
But if you check the sapphire code for how we handle interactions (listeners) should be able to find it fairly easily
We have our own interaction create
alright i'll do that thanks
it's UnknownChatInputCommand
dont forget to "mark solved" whatever helped you
how
@Favna Using this event, log's nothing
name of the file?
and did you rebuild TS code this time?
UnknownChatInputCommand
yes
change it to a small letter u at the start
or set the event in the constructor
and if you do the former remember to nuke dist bet ween rebuilds, tsc wont delete old files
That's actually so dumb
changing the file name, then running tsc
change nothing on the dist
it's not. The file name has to match the name of the event and the event is camelCased not PascalCased
No no, im talking about typescript
Thank you so much it worked :) @Favna