Weird bug (?) with typescript and SlashCommandBuilders

I've ran into a weird issue with ts and SlashCommandBuilders
5 Replies
d.js toolkit
d.js toolkit13mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
treble/luna
treble/luna13mo ago
(copied from #djs-help-v14 ) https://bork.treble-is-fluffy.gay/floof69c30ca6.png I'm getting this ts error with the following code. Whenever i add another options it goes away and i dont really have a clue what is going on since i just typed it as SlashCommandBuilder.
data: new SlashCommandBuilder()
.setName('deleteanon').setDMPermission(false)
.setDescription('Delete your anonymous message')
.addStringOption( opt =>
opt
.setName('message')
.setDescription('The ID of the message (not link!)')
.setMaxLength(19)
.setRequired(true)
)
data: new SlashCommandBuilder()
.setName('deleteanon').setDMPermission(false)
.setDescription('Delete your anonymous message')
.addStringOption( opt =>
opt
.setName('message')
.setDescription('The ID of the message (not link!)')
.setMaxLength(19)
.setRequired(true)
)
Now, this also happens with commands that have sub commands
data: new SlashCommandBuilder()
.setName('user') /*.setDMPermission(false)*/
.setDefaultMemberPermissions(PermissionsBitField.Flags.ModerateMembers)
.setDescription('User interface')

.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('kick')
.addUserOption(option =>
option
.setName("user")
.setDescription("The user to kick")
.setRequired(true)
)
.addStringOption(o =>
o.setName('reason')
.setDescription('The reason')
)
.setDescription("Kick someone")
)

.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('warn')
.addMentionableOption(option =>
option
.setName("user")
.setDescription("The user to warn")
.setRequired(true)
)
.addStringOption(o =>
o.setName('reason')
.setDescription('The reason')
.setRequired(true)
)
.setDescription("Warn someone")
)

.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('ban')
.addUserOption(option =>
option
.setName("user")
.setDescription("The user to ban")
.setRequired(true)
)
.addBooleanOption(option =>
option
.setName("purge")
.setDescription('Delete messages')
)
.addStringOption(o =>
o.setName('reason')
.setDescription('The reason')
)
.setDescription("Ban someone")
)


.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('getwarns')
.addUserOption(option =>
option
.setName("user")
.setDescription("The user to lookup")
.setRequired(true)
)

.setDescription("Get the warns of a user")
)

.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('mute')
.addUserOption(option =>
option
.setName("user")
.setDescription("The user to mute")
.setRequired(true)
)
.addStringOption(option =>
option
.setName("duration")
.setDescription('The duration of the mute')
.setRequired(true)
)
.addStringOption(option =>
option
.setName('timeunit')
.setDescription('The timeunit')
.addChoices({
name: "Seconds", value: "s" },
{name: "Minutes", value: "m"},
{name: "Hours", value: "h"},
{name: "Days", value: "d"},
{name: "Weeks", value:"w"})
.setRequired(true)
)
.addStringOption(o =>
o.setName('reason')
.setDescription('The reason')
)

.setDescription("Mute someone")
).setDMPermission(false)
data: new SlashCommandBuilder()
.setName('user') /*.setDMPermission(false)*/
.setDefaultMemberPermissions(PermissionsBitField.Flags.ModerateMembers)
.setDescription('User interface')

.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('kick')
.addUserOption(option =>
option
.setName("user")
.setDescription("The user to kick")
.setRequired(true)
)
.addStringOption(o =>
o.setName('reason')
.setDescription('The reason')
)
.setDescription("Kick someone")
)

.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('warn')
.addMentionableOption(option =>
option
.setName("user")
.setDescription("The user to warn")
.setRequired(true)
)
.addStringOption(o =>
o.setName('reason')
.setDescription('The reason')
.setRequired(true)
)
.setDescription("Warn someone")
)

.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('ban')
.addUserOption(option =>
option
.setName("user")
.setDescription("The user to ban")
.setRequired(true)
)
.addBooleanOption(option =>
option
.setName("purge")
.setDescription('Delete messages')
)
.addStringOption(o =>
o.setName('reason')
.setDescription('The reason')
)
.setDescription("Ban someone")
)


.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('getwarns')
.addUserOption(option =>
option
.setName("user")
.setDescription("The user to lookup")
.setRequired(true)
)

.setDescription("Get the warns of a user")
)

.addSubcommand((cmd : SlashCommandSubcommandBuilder) =>
cmd
.setName('mute')
.addUserOption(option =>
option
.setName("user")
.setDescription("The user to mute")
.setRequired(true)
)
.addStringOption(option =>
option
.setName("duration")
.setDescription('The duration of the mute')
.setRequired(true)
)
.addStringOption(option =>
option
.setName('timeunit')
.setDescription('The timeunit')
.addChoices({
name: "Seconds", value: "s" },
{name: "Minutes", value: "m"},
{name: "Hours", value: "h"},
{name: "Days", value: "d"},
{name: "Weeks", value:"w"})
.setRequired(true)
)
.addStringOption(o =>
o.setName('reason')
.setDescription('The reason')
)

.setDescription("Mute someone")
).setDMPermission(false)
`
Syjalo
Syjalo13mo ago
To declare the data type use Pick<SlashCommandBuilder, 'name' | 'toJSON'> and other properties you actually need.
treble/luna
treble/luna13mo ago
i maybe should've used pastebin ah that fixes it indeed, still weird though that changing the position of .setDMPermissions solved it in my previous code
Aircraft Overviewer
Changing the position of #setDMPermissions() "solved" it before because the type check you had in place was for a SlashCommandBuilder and it seems that setting the DM permissions after a slash command option removes the omitted generic from the instance. As Syjalo already pointed out, adding a generic to account for this in your check removes the issue all together as it allows for this omitted generic to pass the type check as well as a regular SlashCommandBuilder instance :P