Cloning a SlashCommandBuilder
Is there a way to automatically clone a SlashCommandBuilder? Its constructor is empty and there's no clone or .from() method
7 Replies
- 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!You could try
const second_builder = new SlashCommandBuilder(first_builder.data)
mainly for utility, I was writing the serializing logic where I create a builder for the data of the command itself (name, description) and then filling it with subcommands/groups automatically with a build method, but if I call build twice it'd duplicate the commands
I got around it by caching the result and invalidating when a new child is added but I kept wondering if I could've just cloned the original builder instead of mutating it
it looks like its constructor is empty though https://github.com/discordjs/discord.js/blob/main/packages/builders/src/interactions/slashCommands/SlashCommandBuilder.ts
GitHub
discord.js/packages/builders/src/interactions/slashCommands/SlashCo...
A powerful JavaScript library for interacting with the Discord API - discordjs/discord.js
ts also warns about it
oh idk then
builders suck
:kek:
if I made it in the build method I'd have to call super everytime to make sure it actually gets populated with its children though
about the latter, I don't really want to rely on the fact that a method is not being called twice, it doesn't sound that safe
I'd probably have to rename it for that case :Thonk:
if there was a way to reset the options (like a setSubCommands([]), setGroups([]), setChildren([]) or something like that) I could just call that at the beginning of build and it'd work as well
what if another child is added after it's built though
options seems to be readonly but could there be a problem if I just assign it?
for now I'll put the creation of the builder to a separate method instead of a property, so I get a new one every time
I guess that's how a clone method would work like either way
yeah, but it was stored in a field, not in a method (since the "base" data never changes)
what I wanted to clone was the builder with the base data, and populate the clone with the children
yeah, the data builders seemed like a nice and known pattern but it caused some problems at the end lol
at this point I could've just used raw data
but either way everything is pretty much done, I'll just leave it like this for now
I'll leave the thread open just in case there's more recommendations, thanks for the help