Tembeon / omega

Customizable Discord bot for creating LFGs
1 stars 0 forks source link

feat: present `Interactor` #34

Closed Tembeon closed 4 months ago

Tembeon commented 4 months ago

[!TIP] This PR is a part of new system to allow changing commands while bot is active (see #4)

Overview

Interactor

A new way to register commands, which supports creating, updating and deleting commands using InteractorComponents. Main usage still the same for creating commands, call this method:

Future<void> addComponent(InteractorComponent component)

or to register multiple commands at once:

Future<void> addComponents(Set<InteractorComponent> components)

New API

If you need to clear unknown commands that Discord registered for bot, but bot doesn't know anymore about them, call:

Future<void> forgetUnknown()

[!WARNING] This method will remove all components that are not registered in Interactor, so this methods should be called after all components are registered

All other methods remains the same.

InteractorComponent

New way to build commands. Main way to handle commands remains the same, the only exception — all methods now can be async.

Example of creating new command (slash or message):

class DeleteCommandComponent extends InteractorCommandComponent {
  const DeleteCommandComponent();

  @override
  Future<ApplicationCommandBuilder> build(Services services) async {
    return ApplicationCommandBuilder(
      name: 'Удалить LFG',
      type: ApplicationCommandType.message,
    );
  }

  @override
  Future<void> handle(
    String commandName,
    InteractionCreateEvent<ApplicationCommandInteraction> event,
    Services services,
  ) async {
    final lfgManager = services.lfgManager;
    await lfgManager.delete(message.value);

    await event.interaction.respond(
      MessageBuilder(content: 'Ваше LFG удалено.'),
      isEphemeral: true,
    );
  }
}

See code documentation for more.

Other changes

  1. All commands migrated to new InteractorComponent system
  2. Reorganized folders for commands
  3. Deprecate CommandHandler (use Interactor), LFGBotCore (use services.bot)
  4. Rename Dependencies to Services