Type-safety for guild-only slash commands

I understand that ChatInputCommandInteraction#guildId is nullable, but is there some sort of way to assert it's going to be run in a guild? I'm setting .setDMPermission(false) on the builder, do I need to .guildId! everywhere or is there a better method?
Solution:
if (interaction.inGuild()) {
// now it's in guild and you dont need !
}
if (interaction.inGuild()) {
// now it's in guild and you dont need !
}
...
Jump to solution
26 Replies
Solution
Favna
Favna2y ago
if (interaction.inGuild()) {
// now it's in guild and you dont need !
}
if (interaction.inGuild()) {
// now it's in guild and you dont need !
}
https://github.com/discordjs/discord.js/blob/a76c1ddacc5eb80adbf0a70756f451d709b1a000/packages/discord.js/src/structures/BaseInteraction.js#L178-L180 If you really dont want that, then type it with the same type that inGuild makes it: https://github.com/discordjs/discord.js/blob/a76c1ddacc5eb80adbf0a70756f451d709b1a000/packages/discord.js/typings/index.d.ts#L1100
log(n)
log(n)OP2y ago
Thanks!
HellCatVN
HellCatVN2y ago
How about guild
HellCatVN
HellCatVN2y ago
Is there any options for it
Favna
Favna2y ago
by using inGuild? interaction.inGuild literally what I said oh Idk what the error is bro
HellCatVN
HellCatVN2y ago
I have check it but not working
HellCatVN
HellCatVN2y ago
Favna
Favna2y ago
TS probably doesnt propegate it into the for loop
HellCatVN
HellCatVN2y ago
Somehow only work with guildId
Favna
Favna2y ago
if you do const { channels } = interaction.guild above the for loop it probably works
HellCatVN
HellCatVN2y ago
That good Idea Thanks Working now
HellCatVN
HellCatVN2y ago
HellCatVN
HellCatVN2y ago
Oh check it but still not work Just that line not error
Favna
Favna2y ago
that's not what I said you have to grab channels
HellCatVN
HellCatVN2y ago
HellCatVN
HellCatVN2y ago
I don't see different behaviour frome 2 line of code but anyway it still not work
Favna
Favna2y ago
use inCachedGuild
Favna
Favna2y ago
FYI like the original question this is not strictly sapphire related this is all pure DJS
HellCatVN
HellCatVN2y ago
Sorry I just search arround and find this question Above solution work with me
Favna
Favna2y ago
it's alright just yaknow be sure it is cached it would normally always be unless you set up aggressive cache sweepers yourself
HellCatVN
HellCatVN2y ago
Noted
log(n)
log(n)OP2y ago
My original question was Framework-related because it was about "does Command have a safety/subclass for this"
simxnet
simxnet2y ago
Or InteractionType<'cached'>
log(n)
log(n)OP2y ago
Not my question, this is unrelated
simxnet
simxnet2y ago
I was even talking to you? And yes, Is related
log(n)
log(n)OP2y ago
This is not related to Sapphire nor to the question asked I'm OP...
Want results from more Discord servers?
Add your server