getAllMembers method

Future<Map> getAllMembers({
  1. required dynamic chat_id,
  2. bool isDetail = false,
  3. required int clientId,
  4. Duration? delayLoop,
  5. dynamic onProcces(
    1. Map data,
    2. int client_id
    )?,
  6. bool? isUseCache,
  7. Duration? durationCacheExpire,
})

get all members

Implementation

Future<Map> getAllMembers({
  required chat_id,
  bool isDetail = false,
  required int clientId,
  Duration? delayLoop,
  dynamic Function(Map data, int client_id)? onProcces,
  bool? isUseCache,
  Duration? durationCacheExpire,
}) async {
  delayLoop ??= Duration(milliseconds: 500);
  Map chatResult = await getChat(
    chat_id,
    clientId: clientId,
    is_more_detail: true,
    isUseCache: isUseCache,
    durationCacheExpire: durationCacheExpire,
    is_skip_reply_message: true,
  );
  if (chatResult["ok"] == false) {
    return {"@type": "error"};
  }
  Map chat = chatResult["result"];
  late num member_count = 0;
  if (chat["detail"] is Map && chat["detail"]["member_count"] is int) {
    member_count = chat["detail"]["member_count"];
  }
  var (List<int> list_offset, int _) = TgUtils.createOffset(
    totalCount: member_count.toInt(),
    limitCount: 200,
  );
  List user_ids = [];
  for (var i = 0; i < list_offset.length; i++) {
    var loop_data = list_offset[i];
    await Future.delayed(Duration(seconds: 2));
    var getSupergroupMembers = await request(
      "getSupergroupMembers",
      parameters: {
        "supergroup_id": int.parse("${chat["id"]}"
            .replaceAll(RegExp(r"^-100", caseSensitive: false), "")),
        "offset": loop_data,
        "limit": 200,
      },
      isUseCache: isUseCache,
      durationCacheExpire: durationCacheExpire,
      clientId: clientId,
    );
    late List members = [];
    if (getSupergroupMembers["members"] is List) {
      members = getSupergroupMembers["members"];
    }
    for (var ii = 0; ii < members.length; ii++) {
      late Map member = {};
      if (members[ii] is Map) {
        member = members[ii];
      }
      if (member.isNotEmpty) {
        if (member["member_id"] is Map) {
          int memberUserId = member["member_id"]["user_id"];
          if (!user_ids.contains(memberUserId)) {
            if (!isDetail) {
              if (onProcces != null) {
                onProcces.call(
                  {
                    "@type": "user",
                    "id": memberUserId,
                  },
                  0,
                );
              }
            }
            user_ids.add(memberUserId);
          }
        }
      }
    }
  }
  if (isDetail) {
    await Future.delayed(Duration(seconds: 2));
    late List<Map> array = [];
    for (var i = 0; i < user_ids.length; i++) {
      await Future.delayed(delayLoop);
      try {
        int user_id = user_ids[i];
        Map user_detail = await getUser(
          user_id,
          clientId: clientId,
          isUseCache: isUseCache,
          durationCacheExpire: durationCacheExpire,
        );
        array.add(user_detail["result"] as Map);
        if (onProcces != null) {
          onProcces.call(
            user_detail["result"] as Map,
            0,
          );
        }
      } catch (e) {
        array.add({"@type": "error", "user_id": user_ids[i]});
      }
    }
    return {
      "@type": "members",
      "users": array,
    };
  } else {
    return {
      "@type": "members",
      "user_ids": user_ids,
    };
  }
}