Skip to main content

👂 Command Subscriptions

The CommandSubscriptionsContainer is the object responsible for managing command subscriptions. That is, the subscribers that are subscribed to the Client Event Bus, and are listening to interactionCreate events. It's stored by a CommandManager, and you can get it via CommandManager#getSubscriptions().

With this object you can override the manager's command subscribers, letting you add your own custom logic in runtime.

Specifically, it stores the CommandInteractionSubscriber and DefaultCommandAutocompleteSubscriber. The first listens for command and component interactions, routing them to CommandManager#execute() and the second listens for autocomplete interactions, routing them to CommandManager#autocomplete().

👷 Creation

You can create a command subscriptions container by either:

  • Extending DefaultCommandSubscriptionsContainer from @framework (recommended).
  • Implementing the CommandSubscriptionsContainer interface from @core.
class MyCommandSubscriptionsContainer extends DefaultCommandSubscriptionsContainer {
// ...
}

const myContainer = new MyCommandSubscriptionsContainer();

const myBot = Bot.create((bot) => ({
commands: DefaultCommandManager.create(bot, client, clientBus, { subscriptionsContainer: myContainer }),
}));

👂 Replacing command subscribers

You can use this object to override the manager's command subscribers, letting you add your own custom logic in runtime.

note

For more information about creating subscribers, check the 📩 Event Subscribers guide.

class MyCommandInteractionSubscriber extends AbstractDJSClientSubscriber<'interactionCreate'> {
protected override readonly event = Events.InteractionCreate;

public async handleEvent(
meta: EventDispatchMeta,
interaction: Interaction,
): Promise<void> {
// Your custom logic.
}
}

const subscriber = new MyCommandInteractionSubscriber();
await bot.getCommandManager().getSubscriptions().setInteractionSubscriber(subscriber);
danger

This will completely override the default subscriber. If your custom subscriber doesn't perform the logic that it should, your bot could ignore commands completely.

You can check the DefaultCommandInteractionSubscriber and DefaultCommandAutocompleteSubscriber source to see how a command subscriber should work.