discordjs / builders

A collection of builders that you can use when creating your bot.
Apache License 2.0
97 stars 37 forks source link

Ability to add command handler functions #43

Closed Mimickal closed 2 years ago

Mimickal commented 2 years ago

Is your feature request related to a problem? Please describe. I am writing a little helper library that allows builders from this library to double as the structure for a command execution framework. It would be helpful if SlashCommandBuilder and SlashCommandSubcommandBuilder had something like a setHandler(function) method to associate a command execution handler function with a command. Is this something the team considers within the scope of this library?

Describe the ideal solution I would like to bundle a handler function in with a SlashCommandBuilder and SlashCommandSubcommandBuilder. Something like:

const builder = new SlashCommandBuilder()
    .setHandler(function(interaction) { ... });
/*
SlashCommandBuilder {
  options: [],
  name: undefined,
  description: undefined,
  defaultPermission: undefined,
  handler: [Function (anonymous)]
}
*/

Describe alternatives you've considered I am currently injecting this function directly into the builder prototypes. I do this instead of extending the classes so I can avoid needing to reimplement the addSubcommand function to change new SlashCommandSubcommandBuilder() to my subclass. This works, but it feels like a janky hack.

function setHandler(handler) {
    this.handler = handler;
    return this;
}
SlashCommandBuilder.prototype.setHandler = setHandler;

Additional context This data would be to help library builders. It shouldn't modify the toJSON() data of any of the classes.

allandiego commented 2 years ago

I dont think its good idea, the purpose of slash command builder is provide a easier way to build a valid metadata necessary to register the command at Discord servers

vladfrangu commented 2 years ago

You can extend the class yourself and export it in your library if you want to achieve that, however this module was built strictly for validating and generating raw API data for application commands and what not, so I'm not a fan of adding this into the library.

Mimickal commented 2 years ago

Fair enough. I went ahead and wrote something that does this myself. For anybody else looking for functionality like this, I've uploaded it here https://github.com/Mimickal/discordjs-command-registry