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.
No description
Solution:
Only if you use bulk overwrite
Jump to solution
115 Replies
UndiedGFX
UndiedGFX2mo ago
so it don't get removed
it does not get removed
Youssef
Youssef2mo ago
it does
kyra
kyra2mo ago
If you're using BulkOverwrite, yes If you use Overwrite or any other strategy, it shouldn't
Youssef
Youssef2mo ago
im talking about this command, btw
No description
Youssef
Youssef2mo ago
not the command file
UndiedGFX
UndiedGFX2mo ago
you should not create commands like that that is anti-sapphire
Youssef
Youssef2mo ago
Im not registering the other commands like this.
kyra
kyra2mo ago
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)
Youssef
Youssef2mo ago
exactly, thanks for understanding
UndiedGFX
UndiedGFX2mo ago
if a command is created in a guild, it shouldnt be removed after logout right?
Youssef
Youssef2mo ago
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
kyra
kyra2mo ago
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
UndiedGFX
UndiedGFX2mo ago
store in a database
kyra
kyra2mo ago
Why store in a database when Discord gives it to us + we fetch it anyways
kyra
kyra2mo ago
Gimme a sec, Youssef
Youssef
Youssef2mo ago
well i know, i want to see if there is another way to this with sapphire
UndiedGFX
UndiedGFX2mo ago
good lord i didnt delete that repo
Youssef
Youssef2mo ago
Take your time, and thank you so much for helping
UndiedGFX
UndiedGFX2mo ago
the tag response, kyra
kyra
kyra2mo ago
Check this in your bot, @youssef s!ev client.application.commands.cache;
Skyra
Skyra2mo ago
Output: ⏱ 502.26μs
Collection(11) [Map] {
'1205923078627000381' => [ApplicationCommand],
'1205923078627000382' => [ApplicationCommand],
'1226164940306383019' => [ApplicationCommand],
'1226164940306383020' => [ApplicationCommand],
'1226164940306383021' => [ApplicationCommand],
'1226164940306383022' => [ApplicationCommand],
'1226164940306383023' => [ApplicationCommand],
'1226164940847583322' => [ApplicationCommand],
'1226164940847583323' => [ApplicationCommand],
'1226164940847583324' => [ApplicationCommand],
'1226164940847583325' => [ApplicationCommand]
}
Collection(11) [Map] {
'1205923078627000381' => [ApplicationCommand],
'1205923078627000382' => [ApplicationCommand],
'1226164940306383019' => [ApplicationCommand],
'1226164940306383020' => [ApplicationCommand],
'1226164940306383021' => [ApplicationCommand],
'1226164940306383022' => [ApplicationCommand],
'1226164940306383023' => [ApplicationCommand],
'1226164940847583322' => [ApplicationCommand],
'1226164940847583323' => [ApplicationCommand],
'1226164940847583324' => [ApplicationCommand],
'1226164940847583325' => [ApplicationCommand]
}
Youssef
Youssef2mo ago
alright give me a seccond
kyra
kyra2mo ago
It should contain all registered commands for the client
UndiedGFX
UndiedGFX2mo ago
i get it nvm
kyra
kyra2mo ago
The reason why this would work, Undied, is because Youssef is registering in the client scope, see ^
UndiedGFX
UndiedGFX2mo ago
i forgot the way djs cache actually worked
Youssef
Youssef2mo ago
No description
Youssef
Youssef2mo ago
hmm i tried creating a command, restarting the bot , run this it doesn't contain the created command
Youssef
Youssef2mo ago
although when i try it after creating the command exacly (so no restart), it has the command
No description
kyra
kyra2mo ago
Does the custom command exist on Discord when you try /asana?
UndiedGFX
UndiedGFX2mo ago
if i remember, the djs cache adds guild commands when someone runs it, i might be totally wrong as well
kyra
kyra2mo ago
Undied, those aren't guild commands.
Youssef
Youssef2mo ago
yes when i don't restart, it does exist
UndiedGFX
UndiedGFX2mo ago
the /asana is a guild one kyra
kyra
kyra2mo ago
Not a guild command, see the code ^
UndiedGFX
UndiedGFX2mo ago
, interaction?.guild.id
kyra
kyra2mo ago
Ok, sorry, I stand corrected I could barely see that, the font is too small
UndiedGFX
UndiedGFX2mo ago
kyra can we atleast force djs cache to add something to it?
kyra
kyra2mo ago
Well, for guilds, you'd have to fetch with the guild Considering that information, you'll have to store the IDs in the database 😅
Youssef
Youssef2mo ago
kyra, is there way to add the command when i create it to the "collection or the thing" that sapphire use
UndiedGFX
UndiedGFX2mo ago
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
Youssef
Youssef2mo ago
and after that, the problem is more saphire side
UndiedGFX
UndiedGFX2mo ago
discord.js side*
Youssef
Youssef2mo ago
no? why is it djs side?
UndiedGFX
UndiedGFX2mo ago
sapphire doesnt have this "collection" or cache it is purely djs
Youssef
Youssef2mo ago
the command is created correcly, it's when the bot got resttarted
kyra
kyra2mo ago
For fetching guild commands, you need to pass the guild ID to ApplicationCommandManager#fetch
UndiedGFX
UndiedGFX2mo ago
discord,js cache doesnt work the way you think
Favna
Favna2mo ago
the only cache that sapphire has is a list of commands from the commands files and it matches ids from discordjs to them
kyra
kyra2mo ago
Sapphire doesn't know that a command was created under a guild
UndiedGFX
UndiedGFX2mo ago
@youssef if only there was a way you could force the cache
Youssef
Youssef2mo ago
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
kyra
kyra2mo ago
Youssef, you're not being misunderstood Thing is, this is a Discord limitation, not a Discord.js or a Sapphire one
UndiedGFX
UndiedGFX2mo ago
even if you do it without sapphire and only djs, it will be the same limitation
Youssef
Youssef2mo ago
so what should i do exactly?
UndiedGFX
UndiedGFX2mo ago
1) force feed the cache by fetching guild commands on startup
kyra
kyra2mo ago
There's no such endpoint that returns global and ALL guild commands Neither Discord.js nor Sapphire knows which guilds have custom commands
Youssef
Youssef2mo ago
if i register the command globally, will it work?
kyra
kyra2mo ago
Yes
UndiedGFX
UndiedGFX2mo ago
yes
kyra
kyra2mo ago
But it'll be available in all guilds, idk if that's desirable for you Question, why do you need that cache populated?
UndiedGFX
UndiedGFX2mo ago
no point but thats what they asked for
kyra
kyra2mo ago
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
Youssef
Youssef2mo ago
hmm, i just want the command to stay in the discord
UndiedGFX
UndiedGFX2mo ago
it does
kyra
kyra2mo ago
Populating that cache will do nothing, that's the thing Sapphire won't delete it, nor will Discord.js
Youssef
Youssef2mo ago
it does not when i restart the bot, it get removed
UndiedGFX
UndiedGFX2mo ago
@youssef you have to do this, you are forced to; there is no other way round it doesnt get removed, the cache gets refreshed
Youssef
Youssef2mo ago
bro it got deleted when i restarted the bot
kyra
kyra2mo ago
Register the command, restart the bot, restart Discord, tell me if it exists in the UI
UndiedGFX
UndiedGFX2mo ago
from the discord app?
Youssef
Youssef2mo ago
alright yes
UndiedGFX
UndiedGFX2mo ago
it shouldnt, right?
Youssef
Youssef2mo ago
it got removed after restaring
UndiedGFX
UndiedGFX2mo ago
youssef would you mind if i take a look on the server?
Youssef
Youssef2mo ago
wait im gonna share the video
Youssef
Youssef2mo ago
No description
Youssef
Youssef2mo ago
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.
kyra
kyra2mo ago
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
vladdy
vladdy2mo ago
Only if you use bulk overwrite
vladdy
vladdy2mo ago
Otherwise we do not delete commands, never have, never will
kyra
kyra2mo ago
That's the first thing I said, here ^ So...
vladdy
vladdy2mo ago
Yeah well that's the only place where we actually may delete commands
kyra
kyra2mo ago
Youssef, are you using BulkOverwrite?
Youssef
Youssef2mo ago
I don't think so
Youssef
Youssef2mo ago
No description
Youssef
Youssef2mo ago
that's my client class
kyra
kyra2mo ago
BulkOverwrite isn't written there
vladdy
vladdy2mo ago
Wait what's the issue
Youssef
Youssef2mo ago
No description
vladdy
vladdy2mo ago
I'm omw from an exam so bare with stupid questions
kyra
kyra2mo ago
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 ^
vladdy
vladdy2mo ago
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
kyra
kyra2mo ago
It's TS, yes
vladdy
vladdy2mo ago
or they have a listener that deletes commands
Youssef
Youssef2mo ago
im checking the files just a sec
Youssef
Youssef2mo ago
Actually yes 🥲
No description
Youssef
Youssef2mo ago
removing it, should fix the problem i guess. what should behaviour should i put instead?
UndiedGFX
UndiedGFX2mo ago
.Overwrite
kyra
kyra2mo ago
Overwrite is fine, yes
vladdy
vladdy2mo ago
Overwrite or VerboseOverwrite Or none Default is overwrite anyways
Youssef
Youssef2mo ago
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 🩵🩷🤍🩷🩵?
Favna
Favna2mo ago
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
Youssef
Youssef2mo ago
alright i'll do that thanks it's UnknownChatInputCommand
UndiedGFX
UndiedGFX2mo ago
dont forget to "mark solved" whatever helped you
Youssef
Youssef2mo ago
how
Youssef
Youssef2mo ago
@Favna Using this event, log's nothing
No description
Favna
Favna2mo ago
name of the file? and did you rebuild TS code this time?
Youssef
Youssef2mo ago
UnknownChatInputCommand yes
Favna
Favna2mo ago
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
Youssef
Youssef2mo ago
That's actually so dumb changing the file name, then running tsc change nothing on the dist
Favna
Favna2mo ago
it's not. The file name has to match the name of the event and the event is camelCased not PascalCased
Youssef
Youssef2mo ago
No no, im talking about typescript Thank you so much it worked :) @Favna