parse method

Interaction parse(
  1. Map<String, Object?> raw
)

Implementation

Interaction<dynamic> parse(Map<String, Object?> raw) {
  final type = InteractionType.parse(raw['type'] as int);
  final guildId = maybeParse(raw['guild_id'], Snowflake.parse);
  final channelId = maybeParse(raw['channel_id'], Snowflake.parse);
  final id = Snowflake.parse(raw['id']!);
  final applicationId = Snowflake.parse(raw['application_id']!);
  final channel = maybeParse(raw['channel'], (Map<String, Object?> raw) => client.channels[Snowflake.parse(raw['id']!)]);
  // Don't use a tearoff so we don't evaluate `guildId!` unless member is set.
  final member = maybeParse(raw['member'], (Map<String, Object?> raw) => client.guilds[guildId!].members.parse(raw));
  final user = maybeParse(raw['user'], client.users.parse);
  final token = raw['token'] as String;
  final version = raw['version'] as int;
  // Don't use a tearoff so we don't evaluate `channelId!` unless message is set.
  final message = maybeParse(
    raw['message'],
    (Map<String, Object?> raw) => (client.channels[channelId!] as PartialTextChannel).messages.parse(raw, guildId: guildId),
  );
  final appPermissions = Permissions(int.parse(raw['app_permissions'] as String));
  final locale = maybeParse(raw['locale'], Locale.parse);
  final guildLocale = maybeParse(raw['guild_locale'], Locale.parse);
  final entitlements = parseMany(raw['entitlements'] as List, client.applications[applicationId].entitlements.parse);

  final authorizingIntegrationOwners = maybeParse(
    raw['authorizing_integration_owners'],
    (Map<String, Object?> map) => {
      for (final MapEntry(:key, :value) in map.entries) ApplicationIntegrationType.parse(int.parse(key)): Snowflake.parse(value!),
    },
  );
  final context = maybeParse(raw['context'], InteractionContextType.parse);

  return switch (type) {
    InteractionType.ping => PingInteraction(
        manager: this,
        id: id,
        applicationId: applicationId,
        type: type,
        guildId: guildId,
        channel: channel,
        channelId: channelId,
        member: member,
        user: user,
        token: token,
        version: version,
        message: message,
        appPermissions: appPermissions,
        locale: locale,
        guildLocale: guildLocale,
        entitlements: entitlements,
        authorizingIntegrationOwners: authorizingIntegrationOwners,
        context: context,
      ),
    InteractionType.applicationCommand => ApplicationCommandInteraction(
        manager: this,
        id: id,
        applicationId: applicationId,
        type: type,
        data: parseApplicationCommandInteractionData(raw['data'] as Map<String, Object?>, guildId: guildId, channelId: channelId),
        guildId: guildId,
        channel: channel,
        channelId: channelId,
        member: member,
        user: user,
        token: token,
        version: version,
        message: message,
        appPermissions: appPermissions,
        locale: locale,
        guildLocale: guildLocale,
        entitlements: entitlements,
        authorizingIntegrationOwners: authorizingIntegrationOwners,
        context: context,
      ),
    InteractionType.messageComponent => MessageComponentInteraction(
        manager: this,
        id: id,
        applicationId: applicationId,
        type: type,
        data: parseMessageComponentInteractionData(raw['data'] as Map<String, Object?>, guildId: guildId, channelId: channelId),
        guildId: guildId,
        channel: channel,
        channelId: channelId,
        member: member,
        user: user,
        token: token,
        version: version,
        message: message,
        appPermissions: appPermissions,
        locale: locale,
        guildLocale: guildLocale,
        entitlements: entitlements,
        authorizingIntegrationOwners: authorizingIntegrationOwners,
        context: context,
      ),
    InteractionType.modalSubmit => ModalSubmitInteraction(
        manager: this,
        id: id,
        applicationId: applicationId,
        type: type,
        data: parseModalSubmitInteractionData(raw['data'] as Map<String, Object?>),
        guildId: guildId,
        channel: channel,
        channelId: channelId,
        member: member,
        user: user,
        token: token,
        version: version,
        message: message,
        appPermissions: appPermissions,
        locale: locale,
        guildLocale: guildLocale,
        entitlements: entitlements,
        authorizingIntegrationOwners: authorizingIntegrationOwners,
        context: context,
      ),
    InteractionType.applicationCommandAutocomplete => ApplicationCommandAutocompleteInteraction(
        manager: this,
        id: id,
        applicationId: applicationId,
        type: type,
        data: parseApplicationCommandInteractionData(raw['data'] as Map<String, Object?>, guildId: guildId, channelId: channelId),
        guildId: guildId,
        channel: channel,
        channelId: channelId,
        member: member,
        user: user,
        token: token,
        version: version,
        message: message,
        appPermissions: appPermissions,
        locale: locale,
        guildLocale: guildLocale,
        entitlements: entitlements,
        authorizingIntegrationOwners: authorizingIntegrationOwners,
        context: context,
      ),
  } as Interaction;
}