getModal method

  1. @override
Future<ModalContext> getModal(
  1. {required String title,
  2. required List<TextInputBuilder> components,
  3. Duration? timeout}
)
inherited

Present the user with a modal, wait for them to submit it, and return a context representing that submission.

title is the title of the modal that should be shown to the user.

If timeout is set, this method will complete with an error after timeout has passed.

components are the text inputs that will be presented to the user. The TextInputBuilder.customId can be later used with ModalContext.operator[] to get the value submitted by the user.

Implementation

@override
Future<ModalContext> getModal({
  required String title,
  required List<TextInputBuilder> components,
  Duration? timeout,
}) async {
  if (_delegate != null) {
    if (_delegate is! InteractionInteractiveContext) {
      throw UncaughtCommandsException(
        "Couldn't delegate getModal() to non-interaction context",
        _nearestCommandContext,
      );
    }

    return (_delegate as InteractionInteractiveContext).getModal(
      title: title,
      components: components,
      timeout: timeout,
    );
  }

  final interaction = this.interaction;
  if (interaction is! ModalResponse) {
    throw UncaughtCommandsException(
      'Cannot respond to a context of type $runtimeType with a modal',
      _nearestCommandContext,
    );
  }

  ModalBuilder builder = ModalBuilder(
    customId: ComponentId.generate().toString(),
    title: title,
    components: components.map((textInput) => ActionRowBuilder(components: [textInput])).toList(),
  );

  await (interaction as ModalResponse).respondModal(builder);

  return awaitModal(builder.customId, timeout: timeout);
}