parse static method

ComponentId? parse(
  1. String id
)

Parse a ComponentId received from the API.

This method parses the string returned by a call to toString.

If id was not a ComponentId created by nyxx_commands, such as a manually set custom id, this method will return null.

Implementation

static ComponentId? parse(String id) {
  final parts = id.split('/');

  if (parts.isEmpty || parts.first != 'nyxx_commands') {
    return null;
  }

  final uniqueIdentifier = int.parse(parts[1]);
  final sessionStartTime = DateTime.parse(parts[2]);
  final expiresAt = parts[3] != 'null' ? DateTime.parse(parts[3]) : null;
  final allowedUser = parts[4] != 'null' ? Snowflake.parse(parts[4]) : null;

  final ComponentIdStatus? status;
  if (sessionStartTime != currentSessionStartTime) {
    status = ComponentIdStatus.fromDifferentSession;
  } else if (expiresAt?.isBefore(DateTime.now()) ?? false) {
    status = ComponentIdStatus.expired;
  } else {
    status = ComponentIdStatus.ok;
  }

  return ComponentId(
    expiresAt: expiresAt,
    sessionStartTime: sessionStartTime,
    status: status,
    uniqueIdentifier: uniqueIdentifier,
    allowedUser: allowedUser,
  );
}